javascript - 推送通知仅在注销时有效

标签 javascript android ios cordova push-notification

我们有一个 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。我只是保留了原来的 ID,看来它已经起作用了。

关于javascript - 推送通知仅在注销时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24343777/

相关文章:

javascript - 如何获取偶数数组索引并更改其背景颜色

android - BroadcastReceivers 在监听 BOOT_COMPLETED 时接收到哪个上下文?

php - 使用 GCM 将推送通知发送到我的 Android 应用程序的所有设备

ios - swift - if 语句和数组

javascript - JQuery 未初始化对象

javascript - 将 Laravel 数据传递给 Vue 组件

javascript - Underscore debounce vs vanilla Javascript setTimeout

android - java.lang.NoClassDefFoundError : org. apache.http.params.SyncBasicHttpParams

ios - 如何使用UIStackView按比例缩放大小?

ios - IBAction 按钮按下(不想看到按下)