What to do if extension parameters for iOS notifications cannot be retrieved

更新时间:
复制 MD 格式

The following examples show how to retrieve notification extension parameters from the iOS notification payload.

  1. // Push an iOS notification using the advanced OpenAPI interface.

  2. PushRequest pushRequest = new PushRequest();

  3. pushRequest.setAppKey(appKey);

  4. pushRequest.setTarget("DEVICE");

  5. pushRequest.setTargetValue("xxxxxx");

  6. pushRequest.setPushType("NOTICE");

  7. pushRequest.setDeviceType("iOS");

  8. pushRequest.setTitle("Push Title");

  9. pushRequest.setBody("Push Body");

  10. // Pass the notification extension properties in a JSON map format.

  11. // In this example, the extension properties are key1=value1 and key2=value2.

  12. 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:

  1. - (void)handleiOS10Notification:(UNNotification *)notification {

  2. UNNotificationRequest *request = notification.request;

  3. UNNotificationContent *content = request.content;

  4. NSDictionary *userInfo = content.userInfo;

  5. // Notification time

  6. NSDate *noticeDate = notification.date;

  7. // Title

  8. NSString *title = content.title;

  9. // Subtitle

  10. NSString *subtitle = content.subtitle;

  11. // content

  12. NSString *body = content.body;

  13. // Badge

  14. int badge = [content.badge intValue];

  15. // Retrieve the content of custom notification fields. For example, retrieve the content for the keys "key1" and "key2".

  16. NSString *extKey1 = @"key1";

  17. NSString *extKey2 = @"key2";

  18. NSString *extValue1 = [userInfo valueForKey:extKey1];

  19. NSString *extValue1 = [userInfo valueForKey:extKey1];

  20. // Report that the notification was opened.

  21. [CloudPushSDK sendNotificationAck:userInfo];

  22. NSLog(@"Notification, date: %@, title: %@, subtitle: %@, body: %@, badge: %d, extras: [%@ = %@, %@ = %@].", noticeDate, title, subtitle, body, badge, extKey1, extValue1, extKey2, extValue2);

  23. }

iOS 10

  1. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {

  2. NSLog(@"Receive one notification.");

  3. // Retrieve the Apple Push Notification service (APNS) notification content.

  4. NSDictionary *aps = [userInfo valueForKey:@"aps"];

  5. // content

  6. NSString *content = [aps valueForKey:@"alert"];

  7. // Badge count

  8. NSInteger badge = [[aps valueForKey:@"badge"] integerValue];

  9. // Play sound.

  10. NSString *sound = [aps valueForKey:@"sound"];

  11. // Retrieve the content of custom notification fields. For example, retrieve the content for the keys "key1" and "key2".

  12. NSString *extKey1 = @"key1";

  13. NSString *extKey2 = @"key2";

  14. NSString *extValue1 = [userInfo valueForKey:extKey1];

  15. NSString *extValue1 = [userInfo valueForKey:extKey1];

  16. NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@ = %@, %@ = %@]", content, (long)badge, sound, extKey1, extValue1, extKey2, extValue2);

  17. // Clear the iOS badge.

  18. application.applicationIconBadgeNumber = 0;

  19. // Report that the notification was opened.

  20. // [CloudPushSDK handleReceiveRemoteNotification:userInfo]; // Deprecated since v1.8.1

  21. [CloudPushSDK sendNotificationAck:userInfo];