2

pushPlugin.register 函数被 Android 调用,而不是 iOS。这是我的代码。

this.initialize 被调用,我在那里看到第一个警报 - alert('PushNotifications:initialize');

有任何想法吗?为什么 pushPlugin.register 和 window.onNotificationAPN 函数似乎没有被调用?有一点它正在工作,IIRC。我不确定发生了什么变化。

这是我的配置设置: https ://gist.github.com/adaptivedev/d33f38cd3d6cf10be9dc

谢谢!

.service('PushNotifications', function(Utility, $cordovaToast, $rootScope) {

alert('PushNotifications');

var pushPlugin = null;

/*
this.deviceRegId = function() {
    $rootScope.getFDeviceId();
}
*/  

this.initialize = function() {

    alert('PushNotifications:initialize');
    document.addEventListener('deviceready', function() {
        //alert('PushNotifications:initialize:deviceready:device='+JSON.stringify(device));
        pushPlugin = window.plugins.pushNotification;

        if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
            pushPlugin.register(
                function(result) {          
                    console.log('PushNotifications:initialize:1:result='+result);
                },
                function(result) {
                    alert('PushNotifications:initialize:2:result='+result);
                    console.log(JSON.stringify(result));
                },
                {
                    "senderID":"123",
                    "ecb":"onNotificationGCM"
                });
        } else {
            pushPlugin.register(
                function(result) {
                    alert('PushNotifications:initialize:1:result='+result);
                    //$rootScope.setDeviceId(result)
                },
                function(result) {
                    alert('PushNotifications:initialize:2:result='+result);
                    console.log(JSON.stringify(result));
                },
                {
                    "badge":"true",
                    "sound":"true",
                    "alert":"true",
                    "ecb":"onNotificationAPN"
                });
        }

    });

    // notifications for Android
    window.onNotificationGCM = function(e) {
        alert('onNotificationGCM:e='+JSON.stringify(e));
        window.boosterNotification = e;                     
        switch( e.event )
        {
        case 'registered':
            if ( e.regid.length > 0 )
            {
              $rootScope.setDeviceId(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.
                //var my_media = new Media("/android_asset/www/"+ soundfile);
                //my_media.play();
            }
            else
            {  // otherwise we were launched because the user touched a notification in the notification tray.
                if ( e.coldstart )
                {
                    //
                }
                else
                {
                    //
                }
            }

            var msg = e.payload.message.replace(/<b>/g, "")
            msg = msg.replace(/<\/b>/g, "");
            $cordovaToast.showShortCenter(msg).then(function(success) {
                    //$state.go('app.upcoming');
                    $rootScope.updateNotifications();
                  }, function (error) {
                    // error
                }
            );
           //alert(e.payload.message); 
           //Only works for GCM
           // e.payload.msgcnt + '</li>');
           //Only works on Amazon Fire OS
           // e.payload.timeStamp
        break;

        case 'error':
            //e.msg 
        break;

        default:
            // Unknown
        break;
      }
    };

    // notifications for iOS
    window.onNotificationAPN = function(result) {
        alert('onNotificationAPN:result:1:='+JSON.stringify(result));

        if ( event.alert )
        {
            //navigator.notification.alert(event.alert);
        }

        if ( event.sound )
        {
            //var snd = new Media(event.sound);
            //snd.play();
        }

        if ( event.badge )
        {
            //.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
        }

        console.log('onNotificationAPN:result='+JSON.stringify(result));

        window.boosterNotification = result;
    };

};

});

4

1 回答 1

0

我解决了!在 Xcode 中,选择文件 (PushPlugin.m) 并在右侧选中“Target Membership”

于 2014-11-19T17:28:54.220 回答