通知不适用于 ios8 - Pushwoosh + phonegap

Posted

技术标签:

【中文标题】通知不适用于 ios8 - Pushwoosh + phonegap【英文标题】:Notifications not working with ios8 - Pushwoosh + phonegap 【发布时间】:2014-10-06 14:35:29 【问题描述】:

在苹果商店和安卓市场中使用 pushwoosh + phonegap 构建创建的 4 个应用程序。通知在所有设备上都很好,除了新的 ios8。为什么?有什么改变吗?我使用了这两个文件: Pushnotification.js

(function(cordova) 

    function PushNotification() 

    // Call this to register for push notifications and retreive a deviceToken
    PushNotification.prototype.registerDevice = function(config, success, fail) 
        cordova.exec(success, fail, "PushNotification", "registerDevice", config ? [config] : []);
    ;

    // Call this to set tags for the device
    PushNotification.prototype.setTags = function(config, success, fail) 
        cordova.exec(success, fail, "PushNotification", "setTags", config ? [config] : []);
    ;

    // Call this to send geo location for the device
    PushNotification.prototype.sendLocation = function(config, success, fail) 
        cordova.exec(success, fail, "PushNotification", "sendLocation", config ? [config] : []);
    ;

    PushNotification.prototype.onDeviceReady = function() 
        cordova.exec(null, null, "PushNotification", "onDeviceReady", []);
    ;

    // Call this to get tags for the device
    PushNotification.prototype.getTags = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "getTags", []);
    ;

    //android Only----
    PushNotification.prototype.unregisterDevice = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "unregisterDevice", []);
    ;

    //config params: msg:"message", seconds:30, userData:"optional"
    PushNotification.prototype.createLocalNotification = function(config, success, fail) 
        cordova.exec(success, fail, "PushNotification", "createLocalNotification", config ? [config] : []);
    ;

    PushNotification.prototype.clearLocalNotification = function() 
        cordova.exec(null, null, "PushNotification", "clearLocalNotification", []);
    ;

    //advanced background task to track device position and not drain the battery
    PushNotification.prototype.startGeoPushes = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "startGeoPushes", []);
    ;

    PushNotification.prototype.stopGeoPushes = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "stopGeoPushes", []);
    ;

    //sets multi notification mode on
    PushNotification.prototype.setMultiNotificationMode = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "setMultiNotificationMode", []);
    ;

    //sets single notification mode
    PushNotification.prototype.setSingleNotificationMode = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "setSingleNotificationMode", []);
    ;

    //type: 0 default, 1 no sound, 2 always
    PushNotification.prototype.setSoundType = function(type, success, fail) 
        cordova.exec(success, fail, "PushNotification", "setSoundType", [type]);
    ;  

    //type: 0 default, 1 no vibration, 2 always
    PushNotification.prototype.setVibrateType = function(type, success, fail) 
        cordova.exec(success, fail, "PushNotification", "setVibrateType", [type]);
    ;  

    PushNotification.prototype.setLightScreenOnNotification = function(on, success, fail) 
        cordova.exec(success, fail, "PushNotification", "setLightScreenOnNotification", [on]);
    ;

    //set to enable led blinking when notification arrives and display is off
    PushNotification.prototype.setEnableLED = function(on, success, fail) 
        cordova.exec(success, fail, "PushNotification", "setEnableLED", [on]);
    ;

    //goal:'name', count:3 (count is optional)
    PushNotification.prototype.sendGoalAchieved = function(config, success, fail) 
        cordova.exec(success, fail, "PushNotification", "sendGoalAchieved", config ? [config] : []);
    ;

    //Android End----

    //iOS only----
    PushNotification.prototype.startLocationTracking = function(backgroundMode, success, fail) 
        cordova.exec(success, fail, "PushNotification", "startLocationTracking", backgroundMode ? [mode : backgroundMode] : []);
    ;

    PushNotification.prototype.stopLocationTracking = function(success, fail) 
        cordova.exec(success, fail, "PushNotification", "stopLocationTracking", []);
    ;

    // Call this to get a detailed status of remoteNotifications
    PushNotification.prototype.getRemoteNotificationStatus = function(callback) 
        cordova.exec(callback, callback, "PushNotification", "getRemoteNotificationStatus", []);
    ;

    // Call this to set the application icon badge
    PushNotification.prototype.setApplicationIconBadgeNumber = function(badgeNumber, callback) 
        cordova.exec(callback, callback, "PushNotification", "setApplicationIconBadgeNumber", [badge: badgeNumber]);
    ;

    // Call this to clear all notifications from the notification center
    PushNotification.prototype.cancelAllLocalNotifications = function(callback) 
        cordova.exec(callback, callback, "PushNotification", "cancelAllLocalNotifications", []);
    ;
    //iOS End----

    // Event spawned when a notification is received while the application is active
    PushNotification.prototype.notificationCallback = function(notification) 
        var ev = document.createEvent('htmlEvents');
        ev.notification = notification;
        ev.initEvent('push-notification', true, true, arguments);
        document.dispatchEvent(ev);
    ;

    cordova.addConstructor(function() 
        if(!window.plugins) window.plugins = ;
        window.plugins.pushNotification = new PushNotification();
    );

)(window.cordova || window.Cordova || window.PhoneGap);

还有 app.js

    function registerPushwooshIOS() 
        var pushNotification = window.plugins.pushNotification;

    //push notifications handler
    document.addEventListener('push-notification', function(event) 
                var notification = event.notification;
                //navigator.notification.alert(notification.aps.alert);

                //to view full push payload
                //navigator.notification.alert(JSON.stringify(notification));

                //reset badges on icon
                pushNotification.setApplicationIconBadgeNumber(0);
              );

    pushNotification.registerDevice(alert:true, badge:true, sound:true, pw_appid:"69620-5C1D0", appname:"Maxistore",
                                    function(status) 
                                        var deviceToken = status['deviceToken'];
                                        console.warn('registerDevice: ' + deviceToken);
                                        onPushwooshiOSInitialized(deviceToken);
                                    ,
                                    function(status) 
                                        console.warn('failed to register : ' + JSON.stringify(status));
                                        navigator.notification.alert(JSON.stringify(['failed to register ', status]));
                                    );

    //reset badges on start
    pushNotification.setApplicationIconBadgeNumber(0);


function onPushwooshiOSInitialized(pushToken)

    var pushNotification = window.plugins.pushNotification;
    //retrieve the tags for the device
    pushNotification.getTags(function(tags) 
                                console.warn('tags for the device: ' + JSON.stringify(tags));
                             ,
                             function(error) 
                                console.warn('get tags error: ' + JSON.stringify(error));
                             );

    //start geo tracking. PWTrackSignificantLocationChanges - Uses GPS in foreground, Cell Triangulation in background. 
    pushNotification.startLocationTracking('PWTrackSignificantLocationChanges',
                                    function() 
                                           console.warn('Location Tracking Started');
                                    );


function registerPushwooshAndroid() 

    var pushNotification = window.plugins.pushNotification;

    //push notifications handler
    document.addEventListener('push-notification', function(event) 
                var title = event.notification.title;
                var userData = event.notification.userdata;

                //dump custom data to the console if it exists
                if(typeof(userData) != "undefined") 
                    console.warn('user data: ' + JSON.stringify(userData));
                

                //and show alert
                //navigator.notification.alert(title);

                //stopping geopushes
                pushNotification.stopGeoPushes();
              );

    //projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID"
    pushNotification.registerDevice( projectid: "863823034249", appid : "69620-5C1D0" ,
                                    function(token) 
                                        alert(token);
                                        //callback when pushwoosh is ready
                                        onPushwooshAndroidInitialized(token);
                                    ,
                                    function(status) 
                                        alert("failed to register: " +  status);
                                        console.warn(JSON.stringify(['failed to register ', status]));
                                    );
 

function onPushwooshAndroidInitialized(pushToken)

    //output the token to the console
    console.warn('push token: ' + pushToken);

    var pushNotification = window.plugins.pushNotification;

    pushNotification.getTags(function(tags) 
                            console.warn('tags for the device: ' + JSON.stringify(tags));
                         ,
                         function(error) 
                            console.warn('get tags error: ' + JSON.stringify(error));
                         );


    //set multi notificaiton mode
    //pushNotification.setMultiNotificationMode();
    //pushNotification.setEnableLED(true);

    //set single notification mode
    //pushNotification.setSingleNotificationMode();

    //disable sound and vibration
    //pushNotification.setSoundType(1);
    //pushNotification.setVibrateType(1);

    pushNotification.setLightScreenOnNotification(false);

    //goal with count
    //pushNotification.sendGoalAchieved(goal:'purchase', count:3);

    //goal with no count
    //pushNotification.sendGoalAchieved(goal:'registration');

    //setting list tags
    //pushNotification.setTags("MyTag":["hello", "world"]);

    //settings tags
    pushNotification.setTags(deviceName:"hello", deviceId:10,
                                    function(status) 
                                        console.warn('setTags success');
                                    ,
                                    function(status) 
                                        console.warn('setTags failed');
                                    );

    function geolocationSuccess(position) 
        pushNotification.sendLocation(lat:position.coords.latitude, lon:position.coords.longitude,
                                 function(status) 
                                      console.warn('sendLocation success');
                                 ,
                                 function(status) 
                                      console.warn('sendLocation failed');
                                 );
    ;

    // onError Callback receives a PositionError object
    //
    function geolocationError(error) 
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    

    function getCurrentPosition() 
        navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);
    

    //greedy method to get user position every 3 second. works well for demo.
//  setInterval(getCurrentPosition, 3000);

    //this method just gives the position once
//  navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);

    //this method should track the user position as per Phonegap docs.
//  navigator.geolocation.watchPosition(geolocationSuccess, geolocationError,  maximumAge: 3000, enableHighAccuracy: true );

    //Pushwoosh Android specific method that cares for the battery
    pushNotification.startGeoPushes();


 function initPushwoosh() 
    var pushNotification = window.plugins.pushNotification;
    if(device.platform == "Android")
    
        registerPushwooshAndroid();
        pushNotification.onDeviceReady();
    

    if(device.platform == "iPhone" || device.platform == "iOS")
    
        registerPushwooshIOS();
        pushNotification.onDeviceReady();
    

【问题讨论】:

【参考方案1】:

iOS8 中的推送通知界面发生了变化。如果您的第三方库尚未更新以适应,推送通知可能会失败。

背景: 以前,注册通知是通过

-[UIApplication registerForRemoteNotificationTypes:]

从 iOS8 开始,它现在完成了:

-[UIApplication registerUserNotificationSettings:]
-[UIApplication registerForRemoteNotifications]

【讨论】:

Tnx 为您解答,我使用了最后一个版本的 phonegap...所以我认为该错误在我使用的 2 个文件中。没有人可以帮助我吗?【参考方案2】:

看看pushwoosh post

由于关于 iOS 8 的多个支持请求,我们认为最好在我们的博客中也提供一些信息。目前 Pushwoosh iOS SDK 有两个版本:

iOS 7 SDK - 必须使用 Xcode v.5 编译 - 不支持 iOS 8 中引入的最新功能;

iOS 8 SDK – 必须使用 Xcode v.6 编译

因此,在 Xcode v.5 中使用 iOS 7 SDK 编译的应用程序将继续在运行 iOS 8 的设备上正常运行,因为它具有兼容性。如果您使用 Pushwoosh iOS 8 SDK,您必须使用 Xcode v.6 编译您的应用程序。

你可以下载它here

【讨论】:

以上是关于通知不适用于 ios8 - Pushwoosh + phonegap的主要内容,如果未能解决你的问题,请参考以下文章

推送通知在开发和生产模式下如何工作(pushwoosh)

从 php 通过 pushwoosh 发送推送通知

如何使用 nodejs 发送 pushwoosh 通知?

Pushwoosh 为 windows Phone 8.1 推送 Toast 通知

PushWoosh 通知回调未触发

UITouch 不适用于 ios8