【问题标题】:Push notifications only work when logged out推送通知仅在注销时有效
【发布时间】:2014-06-21 16:50:50
【问题描述】:

我们有一个接收推送通知的 phoengap 应用程序。目前有一个非常奇怪的,当用户退出应用程序时,推送通知工作正常。但是当他们登录时,它会中断。

我想知道它是否在重新注册 GCM/Apple 并在登录时获得新 ID 或其他东西。

我想问题是,我应该什么时候运行以下代码。目前它在用户登录后运行。我应该运行一次然后保存 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;
        }
    }

【问题讨论】:

  • 你解决了这个问题吗?
  • 是的,每次打开应用程序时都会提供一个新 ID,我停止这样做
  • 您能详细说明一下吗?具体如何?
  • 如果你能回答你自己的问题'我什么时候应该运行以下代码',它肯定也会对我有所帮助。

标签: javascript android ios cordova push-notification


【解决方案1】:

这是我的错。每次应用打开时我都在运行这段代码,它给了我一个新的 ID。我只是保留了我原来的 ID,它似乎已经工作了。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    相关资源
    最近更新 更多