PhoneGap 推送插件不注册 iOS 8 设备

Posted

技术标签:

【中文标题】PhoneGap 推送插件不注册 iOS 8 设备【英文标题】:PhoneGap Push Plugin Does Not Register iOS 8 Device 【发布时间】:2014-09-28 17:13:54 【问题描述】:

插件版本为 2.3.1,ios 8.0,iPhone 5。在 iOS 8 之前一切正常。我升级了插件,在 2.0.5 之前。调用 register 时既没有收到成功也没有收到错误响应:

window.plugins.pushNotification.register(

     // tokenHandler (iOS ony) - called when the device has registeredwith a unique device token.
     function (result) 

        alert('device token = ' + result);


     ,

    function(error) 
       alert('error = ' + JSON.stringify(error));


    , 

      
        "badge":"true",
        "sound":"true",
        "alert":"true",
        "ecb":"onNotificationAPN"
      
    );

【问题讨论】:

【参考方案1】:

查看 github 上的拉取请求。

https://github.com/phonegap-build/PushPlugin/pulls

包含许多 iOS8 修复程序。看起来它们还没有合并到 phonegap-build 存储库中。

iOS8 改变了他们注册和处理推送通知的方式,因此您需要更新一些代码更改以支持 iOS8。

【讨论】:

【参考方案2】:

更改 PushPlugin.m

(void)register:(CDVInvokedUrlCommand*)command; self.callbackId = command.callbackId;

NSMutableDictionary* options = [command.arguments objectAtIndex:0];

UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone; id badgeArg = [options objectForKey:@"badge"]; id soundArg = [options objectForKey:@"sound"]; id alertArg = [options objectForKey:@"alert"];

if ([badgeArg isKindOfClass:[NSString class]]) if ([badgeArg isEqualToString:@"true"]) 通知类型|= UIRemoteNotificationTypeBadge; 否则 if ([badgeArg boolValue]) notificationTypes |= UIRemoteNotificationTypeBadge;

if ([soundArg isKindOfClass:[NSString class]]) if ([soundArg isEqualToString:@"true"]) 通知类型|= UIRemoteNotificationTypeSound; 否则 if ([soundArg boolValue]) notificationTypes |= UIRemoteNotificationTypeSound;

if ([alertArg isKindOfClass:[NSString 类]]) if ([alertArg isEqualToString:@"true"]) 通知类型|= UIRemoteNotificationTypeAlert; 否则 if ([alertArg boolValue]) notificationTypes |= UIRemoteNotificationTypeAlert;

self.callback = [options objectForKey:@"ecb"]; if(self.callback!=nil&&[self.callback 长度]) [[NSUserDefaults 标准用户默认值] setObject:self.callback forKey:@"CallBack"];

if (notificationTypes == UIRemoteNotificationTypeNone) NSLog(@"PushPlugin.register: 推送通知类型设置为无");

isInline = 否;

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];

if (notificationMessage) // 如果有挂起的启动通知 [收到自我通知]; // 继续处理它

**

这样改

**

(void)register:(CDVInvokedUrlCommand*)command; self.callbackId = command.callbackId;

NSMutableDictionary* options = [command.arguments objectAtIndex:0];

UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone; id badgeArg = [options objectForKey:@"badge"]; id soundArg = [options objectForKey:@"sound"]; id alertArg = [options objectForKey:@"alert"];

if ([badgeArg isKindOfClass:[NSString class]]) if ([badgeArg isEqualToString:@"true"]) 通知类型|= UIRemoteNotificationTypeBadge; 否则 if ([badgeArg boolValue]) notificationTypes |= UIRemoteNotificationTypeBadge;

if ([soundArg isKindOfClass:[NSString class]]) if ([soundArg isEqualToString:@"true"]) 通知类型|= UIRemoteNotificationTypeSound; 否则 if ([soundArg boolValue]) notificationTypes |= UIRemoteNotificationTypeSound;

if ([alertArg isKindOfClass:[NSString 类]]) if ([alertArg isEqualToString:@"true"]) 通知类型|= UIRemoteNotificationTypeAlert; 否则 if ([alertArg boolValue]) notificationTypes |= UIRemoteNotificationTypeAlert;

self.callback = [options objectForKey:@"ecb"]; if(self.callback!=nil&&[self.callback 长度]) [[NSUserDefaults 标准用户默认值] setObject:self.callback forKey:@"CallBack"];

if (notificationTypes == UIRemoteNotificationTypeNone) NSLog(@"PushPlugin.register: 推送通知类型设置为无");

isInline = 否;

//[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; //适用于 iPhone 8 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; 别的 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];

if (notificationMessage) // 如果有挂起的启动通知 [收到自我通知]; // 继续处理它

【讨论】:

你能把它变成一个 github repo 的要点或差异吗?这样更容易遵循。

以上是关于PhoneGap 推送插件不注册 iOS 8 设备的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap 在设备上启用推送通知

在 Cordova/Phonegap 上使用 iOS 徽章的 Azure 推送通知

用于单一注册的cordova、Phonegap 推送插件和c#?

未收到 Phonegap iOS 推送通知

带有 iOs 的 Phonegap 推送通知插件

构建后phonegap推送插件不起作用