UNNotificationServiceExtension - 通知停止呈现?

时间:2016-09-20 14:42:29

标签: ios apple-push-notifications

尝试新的UNNotificationServiceExtension并注意有趣的行为。

今天9点左右开始在Xcode上进行测试,通知似乎正在流动。内容被修改,一切都很好。

在13:30,所有通知都停止呈现我的所有应用程序(我使用同一团队的多个应用程序进行测试)。我发送可能100次推送几个小时(调试时的正常行为,我会从不在生产中执行此操作,因为APNS的限制。)

我在一台设备(iPhone 5S)上进行测试,在其他设备(iPhone 6)上进行测试时,内容已被修改。 如果删除"可变内容"关键通知再次开始显示

即使您调试应用程序,发送推送也有限制吗? 有没有人有类似的问题?

这是扩展代码。它与模板几乎相同:

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    self.bestAttemptContent.body = @"Modified content";
    self.contentHandler(self.bestAttemptContent);
}

- (void)serviceExtensionTimeWillExpire {
    self.contentHandler(self.bestAttemptContent);
}

以下是App委托代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [[UNUserNotificationCenter currentNotificationCenter]
   requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
   completionHandler:^(BOOL granted, NSError * _Nullable error) {
     if (granted) {
       [application registerForRemoteNotifications];
     }
   }];

  return YES;
}

-(void)applicationDidBecomeActive:(UIApplication *)application
{
  application.applicationIconBadgeNumber = 0;
}

-(void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  NSLog(@"Token %@",deviceToken);
}

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

这是非常简单的虚拟代码,没有额外的计算。

对于使用javapns库发送我的推送:

HashMap<String, Object> finalContent = ...
JSONObject object = new JSONObject(finalContent);
PushNotificationBigPayload payload = PushNotificationBigPayload.fromJSON(object.toString());

List<PushedNotification> notifications = new ArrayList<PushedNotification>();
try {
    ArrayList<String> tokens = new ArrayList<String>();
    tokens.add("token ....");
    notifications = Push.payload(payload,
            "*.p12", "12345",
            false, tokens);
    for(PushedNotification notification : notifications){
        System.out.println(notification);
    }

} catch (CommunicationException e) {
    System.out.println(e);
} catch (KeystoreException e) {
    System.out.println(e);
} 

编辑:设备重启后,notfications会再次修改内容。

0 个答案:

没有答案