The following examples show how to retrieve notification extension parameters from the iOS notification payload.
// Push an iOS notification using the advanced OpenAPI interface.PushRequest pushRequest = new PushRequest();pushRequest.setAppKey(appKey);pushRequest.setTarget("DEVICE");pushRequest.setTargetValue("xxxxxx");pushRequest.setPushType("NOTICE");pushRequest.setDeviceType("iOS");pushRequest.setTitle("Push Title");pushRequest.setBody("Push Body");// Pass the notification extension properties in a JSON map format.// In this example, the extension properties are key1=value1 and key2=value2.pushRequest.setiOSExtParameters("{\"key1\":\"value1\",\"key2\":\"value2\"}")
The following examples show how to retrieve the extension parameters on the client.
For devices running iOS 10 or later:
- (void)handleiOS10Notification:(UNNotification *)notification {UNNotificationRequest *request = notification.request;UNNotificationContent *content = request.content;NSDictionary *userInfo = content.userInfo;// Notification timeNSDate *noticeDate = notification.date;// TitleNSString *title = content.title;// SubtitleNSString *subtitle = content.subtitle;// contentNSString *body = content.body;// Badgeint badge = [content.badge intValue];// Retrieve the content of custom notification fields. For example, retrieve the content for the keys "key1" and "key2".NSString *extKey1 = @"key1";NSString *extKey2 = @"key2";NSString *extValue1 = [userInfo valueForKey:extKey1];NSString *extValue1 = [userInfo valueForKey:extKey1];// Report that the notification was opened.[CloudPushSDK sendNotificationAck:userInfo];NSLog(@"Notification, date: %@, title: %@, subtitle: %@, body: %@, badge: %d, extras: [%@ = %@, %@ = %@].", noticeDate, title, subtitle, body, badge, extKey1, extValue1, extKey2, extValue2);}
iOS 10
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {NSLog(@"Receive one notification.");// Retrieve the Apple Push Notification service (APNS) notification content.NSDictionary *aps = [userInfo valueForKey:@"aps"];// contentNSString *content = [aps valueForKey:@"alert"];// Badge countNSInteger badge = [[aps valueForKey:@"badge"] integerValue];// Play sound.NSString *sound = [aps valueForKey:@"sound"];// Retrieve the content of custom notification fields. For example, retrieve the content for the keys "key1" and "key2".NSString *extKey1 = @"key1";NSString *extKey2 = @"key2";NSString *extValue1 = [userInfo valueForKey:extKey1];NSString *extValue1 = [userInfo valueForKey:extKey1];NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@ = %@, %@ = %@]", content, (long)badge, sound, extKey1, extValue1, extKey2, extValue2);// Clear the iOS badge.application.applicationIconBadgeNumber = 0;// Report that the notification was opened.// [CloudPushSDK handleReceiveRemoteNotification:userInfo]; // Deprecated since v1.8.1[CloudPushSDK sendNotificationAck:userInfo];