【发布时间】:2014-12-24 19:18:40
【问题描述】:
我看到太多关于如果设备未连接到 xcode 时静默推送通知不起作用的问题,但我找不到真正的答案。 我正在使用 Silent APN 在后台启动一个进程,然后触发本地推送通知
-
服务器发送此信息:
"_metadata" = { bagde = 1; pushText = "semeone has sent you a message!!"; sender = "semeone"; }; aps = { "content-available" = 1; };而 _metadata 是触发本地通知的自定义信息,我没有在 aps 中包含徽章、pushText.. 因为我是静默推送通知。
-
客户端应该在 didReceiveRemoteNotification 中获取信息,
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if(application.applicationState != UIApplicationStateActive ){ if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification { //start a background task UIBackgroundTaskIdentifier preLoadPNTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"Background task to start a process "); }]; //end completionHandler regarding to fetchCompletionHandler completionHandler(UIBackgroundFetchResultNewData); // doing my process...... and fire a local notification if(preLoadPNTask){ NSLog(@"End Background task "); [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask]; preLoadPNTask = 0; } return; } else { NSLog(@"didReceiveRemoteNotification it's NOT the silent notification "); completionHandler(UIBackgroundFetchResultNoData); return; } } else { if(preLoadPNTask){ NSLog(@"End Background task "); [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask]; preLoadPNTask = 0; } completionHandler(UIBackgroundFetchResultNewData); } }
当设备连接到 xcode 时它工作得很好,但是当它没有连接时,didReceiveRemoteNotification 不会启动:(
有什么想法吗?
提前谢谢你!!
【问题讨论】:
-
收到通知后你在执行什么任务?
-
我将 ID 和发件人添加到 NSMutableDictionary,然后我将数据发送到 Worklight [[WL sharedInstance] sendActionToJS:@"silentNotification" withData:dictionary];然后我释放本地通知 localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 徽章; localNotif.alertBody = textAlert; localNotif.userInfo = userInfoData; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif 发布];
-
怎么知道是否调用了didReceiveRemoteNotification方法?
-
我和你@jan遇到了同样的情况。到现在都解决不了。 (iOS 8.1.2)
-
我已经在 iOS 7 和 iOS 8 设备上进行了测试。我在 iOS 8 设备上遇到了同样的问题,但不知道发生了什么。在我的 iOS 7 设备上,一切正常——接收到静默通知(有或没有 xcode)。在我的 iOS 8 设备上,根本没有收到静默通知(没有 Xcode)。不过,它可以 100% 使用 Xcode。如果你们中的任何人找到解决方案,请告诉我。
标签: ios objective-c iphone xcode apple-push-notifications