简体   繁体   English

推送通知仅在注销时有效

[英]Push notifications only work when logged out

We've got a phoengap app that receives push notifications. 我们有一个phoengap应用程序,可以接收推送通知。 Got quite a wierd one at the moment, when the user is logged out of the app the push notifications work fine. 此刻,当用户从应用程序注销时,推送通知工作正常。 But when they login, it breaks. 但是当他们登录时,它坏了。

I'm wondering if it's re-registering with GCM/Apple and getting a new ID or something when logged in. 我想知道它是否正在向GCM / Apple重新注册并在登录时获得新的ID或其他信息。

I guess the question is, when should I run the following code. 我想问题是,什么时候应该运行以下代码。 At the moment it runs once the user is logged in. Should I run it once ever and then save the ID and never run the register again? 现在,一旦用户登录,它就会运行。我是否应该运行一次,然后保存ID,然后再也不运行寄存器了?

 receivedEvent: function(id) {
        var pushNotification = window.plugins.pushNotification;

        if (device.platform == 'android' || device.platform == 'Android') {
            pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"1019013414678","ecb":"app.onNotificationGCM"});
        }
        else {

            pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
        }
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    // iOS
    onNotificationAPN: function(event) {
        var pushNotification = window.plugins.pushNotification;
        console.log("Received a notification! " + event.alert);
        console.log("event sound " + event.sound);
        console.log("event badge " + event.badge);
        console.log("event " + event);
        if (event.alert) {
            navigator.notification.alert(event.alert);

        }

        if (event.sound) {
            var snd = new Media(event.sound);
            snd.play();
        }
    },
    // Android
    onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                  localStorage.setItem("PushID", e.regid)
                  updateID();
                }
                break;

            case 'message':
                // this is the actual push notification. its format depends on the data model
                // of the intermediary push server which must also be reflected in GCMIntentService.java
                alert(e.message);
                break;

            case 'error':
                alert('GCM error = '+e.msg);
                break;

            default:
                alert('An unknown GCM event has occurred');
                break;
        }
    }

It was my mistake. 是我的错 I was running this code every time the app opened and it was giving me a new ID. 每次打开应用程序时,我都在运行此代码,并且为我提供了新的ID。 I just kept the ID i had originally and it seems to have worked. 我只是保留了我原来的ID,而且似乎已经奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么只有在php登录会话处于活动状态时Javascript函数才能工作,而注销后却停止工作? - Why do Javascript functions work only if a php login session is active, but stop working when logged out? 服务工作和推送通知 - Service Work and Push Notifications 推送通知如何工作? - How do push notifications work? 推送 API:移动浏览器不会随时收到推送通知。 仅当 SW 运行时 - Push API: Mobile browsers don't receive push notifications anytime. Only when SW is running Firebase 推送通知点击不起作用 - Firebase push notifications click does not work 网络推送通知点击不起作用 - Web push notifications click doesn't work Cordova Firebase插件:安装新应用程序时,Apple Push通知有时不起作用 - Cordova Firebase plugin: Apple Push notifications do not work sometimes when installing a new app 未安装应用程序时 PWA 推送通知不起作用 - PWA push notifications not working when app is not installed Mysql数据库更改状态时使用Push.js推送通知 - Push notifications with Push.js when Mysql database changes status 用户登录和注销时隐藏和显示项目 - Hide and Show items when user logged in and logged out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM