Ios Can';t使用PubNub SDK发送推送通知
我正在使用用于iOS的PubNub SDK在我的应用程序中实现推送通知。我的构建目标是9.0 我正在学习教程,但是我不能让它工作,我觉得我需要更多的信息来理解这个概念。看看我到目前为止所做的工作: AppDelegate.mIos Can';t使用PubNub SDK发送推送通知,ios,objective-c,pubnub,Ios,Objective C,Pubnub,我正在使用用于iOS的PubNub SDK在我的应用程序中实现推送通知。我的构建目标是9.0 我正在学习教程,但是我不能让它工作,我觉得我需要更多的信息来理解这个概念。看看我到目前为止所做的工作: AppDelegate.m @interface AppDelegate () @property (nonatomic) PubNub *client; @end @interface MessagingViewController () @property (nonatomic) PubN
@interface AppDelegate ()
@property (nonatomic) PubNub *client;
@end
@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];
}
}];
}
}
在didfishlaunchingwithoptions
函数中,我运行此代码来设置推送通知:
/* 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);
}
本教程发布的代码如下所示,这应该放在代理或其他ViewController中吗
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 AppDelegate ()
@property (nonatomic) PubNub *client;
@end
@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];
}];
有人能解释一下,给我一个大概的介绍,或者代码的不同部分属于哪里,需要什么吗?你好,Erik
除了传递给mobilePushPayload参数的字典之外,所有代码都有效。在Apple推送通知的情况下,传递的字典应符合其描述。APNS负载中没有消息键,至少应该有警报和字符串值
因此,如果您想发布,您应该尝试以下方法:
[self.client publish:nil to channel:self.senderId mobilePushPayload:@{@“aps”:@{@“alert”:message}
完成时:^(PNPublishStatus*状态){
//检查请求是否成功完成。
//if(status.isError)//处理修改错误。
//检查“类别”属性以找出可能的问题,因为
//其中一个请求失败。可以使用:[状态重试]重新发送请求;
}];代码>你好,埃里克
除了传递给mobilePushPayload参数的字典之外,所有代码都有效。在Apple推送通知的情况下,传递的字典应符合其描述。APNS负载中没有消息键,至少应该有警报和字符串值
因此,如果您想发布,您应该尝试以下方法:
[self.client publish:nil to channel:self.senderId mobilePushPayload:@{@“aps”:@{@“alert”:message}
完成时:^(PNPublishStatus*状态){
//检查请求是否成功完成。
//if(status.isError)//处理修改错误。
//检查“类别”属性以找出可能的问题,因为
//其中一个请求失败。可以使用:[状态重试]重新发送请求;
}];代码>我明白了,谢谢!此外,addPushNotificationsOnChannels调用是否应该在应用程序委托中进行?@Erik每次获取设备推送令牌时都应该调用此方法。因为只有应用程序代表会直接对此做出响应,所以我们建议在那里进行。我理解。因此,当我想向特定用户发送推送通知时,我可以将其发送到一个ID与用户ID相等的通道,例如?这些频道是一种通知组?@Erik,如果您想向特定用户发送通知,该用户应该具有唯一的频道名称,在该频道上已使用-addPushNotificationsOnChannels:withDevicePushToken:andCompletion:
方法启用推送通知。当适当的字典传递到-publish:toChannel:mobilePushPayload:withCompletion:
中时,对于mobilePushPayload参数,PubNub服务将向用户发送推送通知,用户注册接收在发布API调用期间传递的通道更新。@Erik通道是实时数据流单元,任何与JSON兼容的数据都可以通过它传递。如果提供了mobilePushPayload,PubNub服务将采取额外步骤并向所有使用指定频道名称注册的用户发送通知。我明白了,谢谢!此外,addPushNotificationsOnChannels调用是否应该在应用程序委托中进行?@Erik每次获取设备推送令牌时都应该调用此方法。因为只有应用程序代表会直接对此做出响应,所以我们建议在那里进行。我理解。因此,当我想向特定用户发送推送通知时,我可以将其发送到一个ID与用户ID相等的通道,例如?这些频道是一种通知组?@Erik,如果您想向特定用户发送通知,该用户应该具有唯一的频道名称,在该频道上已使用-addPushNotificationsOnChannels:withDevicePushToken:andCompletion:
方法启用推送通知。当适当的字典传递到-publish:toChannel:mobilePushPayload:withCompletion:
中时,对于mobilePushPayload参数,PubNub服务将向用户发送推送通知,用户注册接收在发布API调用期间传递的通道更新。@Erik通道是实时数据流单元,任何与JSON兼容的数据都可以通过它传递。如果提供了mobilePushPayload,PubNub服务将采取额外步骤并向所有使用指定频道名称注册的用户发送通知。