I've got this code set up in my AppDelegate's didFinishLaunchingWithOptions:
PNConfiguration *pnConfiguration = [PNConfiguration configurationWithPublishKey:publishKey
subscribeKey:subscribeKey];
self.client = [PubNub clientWithConfiguration:pnConfiguration];
/* push notifications */
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
As well as these two methods:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken: %@", deviceToken);
[[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"DeviceToken"];
[self.client addPushNotificationsOnChannels:@[@"apns"] withDevicePushToken:deviceToken andCompletion:^(PNAcknowledgmentStatus *status) {
}];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"%s with error: %@", __PRETTY_FUNCTION__, error);
}
I've added the entitlement to my app ID, and uploaded the certificate to the PubNub console as described here
Here's my code for sending a push notification:
[self.client publish:nil toChannel:@"apns" 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];
}];
However, no notification is displayed on my iPhone when publishing from the iOS simulator. Any idea why this is?
Push Notifications don't work on simulator. You will need to test on a real device.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.