如何在Ionic中单击通知时打开详细信息页面

时间:2015-08-26 12:26:07

标签: push-notification ionic-framework google-cloud-messaging

我使用Ionic平台创建移动应用程序,需要有关此情况的帮助。我可以使用此代码注册,然后可以发送通知。

但是当我从我的应用程序发送有关商店的通知时,如何点击我收到的通知后如何打开该商店的页面?

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 个答案:

答案 0 :(得分:1)

更好的解决方案:

从服务器发送带有额外值名称的推送通知" id"。

收到通知后,应用会保存此"额外值"使用" localStorage.setItem。"。

最后在" onresume"应用程序读取此" id"的值使用localStorage.getItem,并根据值,您可以使用$ state.go(....)打开相应的页面;

实施例: 您想要在名为" profile"的应用中打开该页面。当发送特定推送时.. 您发送包含"额外值",id = profile的通知,按照打开消息的相同方式对平台进行编制。 当app通过app收到你保存这个值window.localStorage.setItem(" page",id); 内部" onresume"事件:

它读取" page"的值... var page = window.localStorage.getItem(" page"); 它重定向到页面 if(page!= null){$ stete.go(' page.name');根据页面的价值。} 它删除了页面window.localStorage.revomeItem(页面)的值(因为如果页面值为null,则应用程序必须正常加载)。 如果应用程序已关闭(不是从后台),则打开应用程序也是如此

代码示例:

//************************************  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

找到here

您可以使用Eddy Verbruggen的LaunchMyApp插件。

只需:

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

URL_SCHEME = ionicapp 的位置,您可以更改为自己的方案。

您可以在http://mcgivery.com/using-custom-url-schemes-ionic-framework-app/

中找到更多信息