无法向iOS发送/接收PubNub推送通知

时间:2016-05-03 20:30:05

标签: ios objective-c push-notification apple-push-notifications pubnub

我尝试使用this教程实现推送通知。与PubNub聊天工作完美。我还可以使用this脚本发送和接收推送通知,但只能使用开发证书,因此我将其提交给PubNub。 (有没有人知道为什么?我已经为我的应用程序创建了两个证书)当我打开应用程序时,我收到了包含所有密钥的消息,就像我在DebugConsole中看到的那样:

{ "message": "Asdas",
  "pn_apns": {
    "aps": {
      "alert": "To Apple and PN Native devices!"
    }
  },
  "senderId": "mySenderId",
  "receiverId": "myReceiverId"
}

我将展示我认为与推送通知相关的所有步骤,所以请说如果我忘记了某些内容或者我做错了什么。

didFinishLaunching

UIUserNotificationType types = (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"pub-key" subscribeKey:@"sub-key"];
self.client = [PubNub clientWithConfiguration:configuration];
[self.client addListener:self];
[self.client subscribeToChannels:@"myChannelId" withPresence:NO];

didRegisterForRemoteNotificationsWithDeviceToken

[self.client addPushNotificationsOnChannels:@[@"apns"] withDevicePushToken:deviceToken andCompletion:^(PNAcknowledgmentStatus *status) { }];

发送PubNub-Chat消息

NSDictionary * dict = @{@"aps": @{@"alert":@"To Apple and PN Native devices!"}};
[self.client publish:@{@"message" : @"Hello!", @"senderId" : @"abc123", @"receiverId" : @"abc124"} toChannel:@"myChannel" mobilePushPayload:dict withCompletion:^(PNPublishStatus *status) {}];

2 个答案:

答案 0 :(得分:2)

我太傻了,无法订阅合适的频道。在 didRegisterForRemoteNotificationsWithDeviceToken ,您应该使用您的唯一渠道ID,而不是@"apns" ...

感谢您的帮助

答案 1 :(得分:1)

Dev和prod推是不可互换的。确保你没有打破

  

“推三法”。

有两组三胞胎:

a) Prod app - Prod gateway - Prod certificate
b) Dev app - Dev gateway - Dev certificate
You can't mix and match these together, if you have any combination that isn't either 3 Production things or 3 Dev things then the push won't work. This is the "Push Rule of Three".

这是prod网关:

  

SSL://gateway.push.apple.com:2195

这是开发网关:

  

SSL://gateway.sandbox.push.apple.com:2195