How to open detail page when click notification in

2019-04-15 01:50发布

问题:

i m using Ionic platform to create a mobile app and need a help about this situation. I can take regId with this code and then can send notification.

But when i send a notification about a shop from my app, how can i open that shop's page when click notification that i get ?

var pushNotification;

function onDeviceReady() {
    document.addEventListener("backbutton", function (e) {}, false);

    var pushNotification = window.plugins.pushNotification;
    if (device.platform == 'android' || device.platform == 'Android' || device.platform ==
        'amazon-fireos') {
        pushNotification.register(successHandler, errorHandler, {
            "senderID": "788xxxxxxxx",
            "ecb": "onNotificationGCM"
        });
    }
}
// handle APNS notifications for iOS

function onNotificationAPN(e) {
    // alert('apn');
    if (e.alert) {
        // showing an alert also requires the org.apache.cordova.dialogs plugin
        navigator.notification.alert(e.alert);
    }
    if (e.sound) {
        // playing a sound also requires the org.apache.cordova.media plugin
        var snd = new Media(e.sound);
        snd.play();
    }
    if (e.badge) {
        pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
    }
}
// handle GCM notifications for Android

//function onNotification(e) {
onNotificationGCM = function (e) {
    //  alert('onnoti');
    switch (e.event) {
        case 'registered':
            if (e.regid.length > 0) {
                // alert('f');
                // Your GCM push server needs to know the regID before it can push to this device
                // here is where you might want to send it the regID for later use.
                alert("regID = " + e.regid);
                $rootScope.regidx = e.regid;


            }
            break;
        case 'message':
            // if this flag is set, this notification happened while we were in the foreground.
            // you might want to play a sound to get the user's attention, throw up a dialog, etc.
            if (e.foreground) {
                // on Android soundname is outside the payload.
                // On Amazon FireOS all custom attributes are contained within payload
                var soundfile = e.soundname || e.payload.sound;
                // if the notification contains a soundname, play it.
                // playing a sound also requires the org.apache.cordova.media plugin
                var my_media = new Media("/android_asset/www/" + soundfile);
                my_media.play();
            }
            break;
        case 'error':
            break;
        default:
            break;
    }
}

function tokenHandler(result) {
    alert('device token = ' + result);
    // Your iOS push server needs to know the token before it can push to this device
    // here is where you might want to send it the token for later use.
}

function successHandler(result) {
    alert('Callback Success! Result = ' + result)
}

function errorHandler(error) {
    alert(error);
}

回答1:

Better solution here:

Send a push notification from your server with an extra value name "id".

When the notification is recieved the app save the value of this "extra value" using "localStorage.setItem.".

Finally inside "onresume" event the app reads the value of this "id" using localStorage.getItem, and acording the value you can open the respective page using $state.go( ....);

Example: You want to open the page in your app named "profile" when a specific push is sent.. You send the notification including an "extra value", id=profile, acording the platform in the same way that is opened the message. When push is recieved by app you save this value window.localStorage.setItem("page", id); Inside "onresume" event:

it read the value of "page" ... var page= window.localStorage.getItem("page"); it redirects to the page if (page!=null) { $stete.go('page.name'); acording the value of page.} It removes the value of page window.localStorage.revomeItem(page) (because If page value is null the app must load normally). The same for opening the app if it was closed (not from background)

Code example:

//************************************  OUTSIDE DEVICE READY
    // handle APNS notifications for iOS
    function onNotificationAPN(e) {
    // storage the e.id value  (the extra value sent in push notification)
    window.localStorage.setItem("push_que", e.id);
    var push_que=e.id;
    // if the push notification is coming inline
    if (e.foreground=="1")
    {
   // storage the e.numero value  (the extra value sent in push notification)
    window.localStorage.setItem("push_que", e.id);
    var push_que=e.id;
   /// some code here to open a message  if a new push is recieved inline
    ;}
    if ( event.alert )
    {
    navigator.notification.alert(event.alert);
    }
    if ( event.sound )
    {
    var snd = new Media(event.sound);
    snd.play();
    }
    if ( event.badge )
    {
    pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
    }
    }

    // handle GCM notifications for Android
    function onNotificationGCM(e) {

                switch( e.event )
                {
                     if (e.foreground)
                     {
    //  if the push is recieved inline
   //  storage the value of  playoad.id,  the extra value sent by push
    window.localStorage.setItem("push_que", e.payload.id);
    var push_que=e.payload.id;

    }
    else
    {
    // otherwise we were launched because the user touched a notification in the notification tray

    if (e.coldstart)
    {
  //  storage the value of  playoad.numero, the extra value sent by push
window.localStorage.setItem("push_que", e.payload.id);

}
else
{
  //  storage the value of  playoad.numero, the extra value sent by push
window.localStorage.setItem("push_que", e.payload.id);

}
}


                    break;

                    case 'error':

                    break;

                    default:

                    break;
                }
            }






    //********************************** END OUTSIDE DEVICE READY

Found here

You can use the LaunchMyApp Plugin by Eddy Verbruggen.

Just:

cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git --variable URL_SCHEME=ionicapp

Where in URL_SCHEME=ionicapp you can change to your own scheme.

You can find more in: http://mcgivery.com/using-custom-url-schemes-ionic-framework-app/