无法使用PubNub SDK发送推送通知

时间:2016-05-20 08:54:55

标签: ios objective-c pubnub

我正在使用适用于iOS的PubNub SDK在我的应用中实现推送通知。我的构建目标是9.0。

我正在关注教程here,但我无法让它工作,我觉得我需要更多信息来理解这个概念。看看我到目前为止所做的事情:

AppDelegate.m

@interface AppDelegate ()

@property (nonatomic) PubNub *client;

@end

didFinishLaunchingWithOptions函数中,我运行此代码来设置推送通知:

/* push notifications */
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken: %@", deviceToken);
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"DeviceToken"];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"%s with error: %@", __PRETTY_FUNCTION__, error);
}

教程发布这样的代码,这应该是委托还是其他ViewControllers?:

PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"demo"
                                                              subscribeKey:@"demo"];
self.client = [PubNub clientWithConfiguration:configuration];
[self.client addPushNotificationsOnChannels:@[@"wwdc",@"google.io"] 
                     withDevicePushToken:self.devicePushToken
                           andCompletion:^(PNAcknowledgmentStatus *status) {

 // Check whether request successfully completed or not.
 if (!status.isError) {

    // Handle successful push notification enabling on passed channels.
 }
 // Request processing failed.
 else {

    // Handle modification error. Check 'category' property to find out possible issue because
    // of which request did fail.
    //
    // Request can be resent using: [status retry];
 }
 }];

ViewController.m

@interface MessagingViewController ()

@property (nonatomic) PubNub *client;
@property (nonatomic, strong) NSData *devicePushToken;

@end

-(void)viewDidLoad {
    NSData *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
if (deviceToken)
{
    self.devicePushToken = deviceToken;
    PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:publishKey
                                                                     subscribeKey:subscribeKey];
    self.client = [PubNub clientWithConfiguration:configuration];
    [self.client addPushNotificationsOnChannels:@[self.senderId]
                            withDevicePushToken:self.devicePushToken
                                  andCompletion:^(PNAcknowledgmentStatus *status) {

                                      // Check whether request successfully completed or not.
                                      if (!status.isError) {

                                          // Handle successful push notification enabling on passed channels.
                                      }
                                      // Request processing failed.
                                      else {

                                          // Handle modification error. Check 'category' property to find out possible issue because
                                          // of which request did fail.
                                          //
                                          // Request can be resent using: [status retry];
                                      }
                                  }];
}
}

代码我用来发送通知:

            /* send push notifications */
        [self.client publish:nil toChannel:self.senderId mobilePushPayload: @{@"aps": @{@"message":message}}
              withCompletion:^(PNPublishStatus *status) {

                  // Check whether request successfully completed or not.
                  // if (status.isError) // Handle modification error.
                  // Check 'category' property to find out possible issue because
                  // of which request did fail. Request can be resent using: [status retry];
              }];

有人可以解释并给我一些概述或代码的不同部分属于哪里以及需要什么?

1 个答案:

答案 0 :(得分:0)

美好的一天Erik,

除了传递给 mobilePushPayload 参数的字典外,所有代码都有效。如果Apple推送通知符合描述specification,则通过字典。 APNS有效负载中没有消息键,至少应该有 alert 和字符串值。

所以,如果你想发布,你应该尝试这样的事情:



[self.client publish:nil toChannel:self.senderId mobilePushPayload: @{@"aps": @{@"alert":message}}
      withCompletion:^(PNPublishStatus *status) {

    // Check whether request successfully completed or not.
    // if (status.isError) // Handle modification error.
    // Check 'category' property to find out possible issue because
    // of which request did fail. Request can be resent using: [status retry];
}];




最好的问候,
塞吉

相关问题