首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ionic中单击通知时如何打开详细信息页

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

Stack Overflow用户
提问于 2015-08-26 12:26:06
回答 1查看 9K关注 0票数 1

我正在使用Ionic平台来创建一个移动应用程序,并需要关于这种情况的帮助。我可以在这段代码中使用regId,然后发送通知。

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

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-26 21:09:44

更好的解决办法如下:

从您的服务器发送一个推送通知,其附加值名称为"id“。

当收到通知时,应用程序使用“localStorage.setItem”保存这个“额外值”的值。

最后,在“on简历”事件中,应用程序使用localStorage.getItem读取这个"id“的值,并按照可以使用$state.go( .)打开相应页面的值;

示例:当发送特定的推送时,您希望在您的应用程序中打开名为"profile“的页面。您发送包含“额外值”( id=profile )的通知,与打开消息的方式相同。当应用程序接收到推送时,您可以保存此值window.localStorage.setItem(“页面”,id);在“on简历”事件中:

上面写着“页面”的价值..。var page= window.localStorage.getItem(" page ");它重定向到页面if ( page != null ) { $stete.go('page.name');根据页面的值。}它移除页面window.localStorage.revomeItem(页面)的值(因为如果页面值为null,应用程序必须正常加载)。如果关闭了应用程序(而不是背景),打开应用程序也是一样的。

代码示例:

代码语言:javascript
复制
//************************************  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

发现这里

您可以使用Eddy的LaunchMyApp插件。

只是:

代码语言:javascript
复制
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/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32226655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档