我的应用在未运行时不会收到推送通知。 我正在尝试处理以 JSON 形式发送的远程通知,并使用给定 JSON 中的数据更新我的应用程序中的数据。 当应用程序处于事件状态或在后台时,一切顺利。 但是当应用程序未运行时,应用程序仅在我通过点击通知打开我的应用程序时处理通知,而不是在我通过点击图标打开应用程序时处理通知。 下面是 appDelegate 类的代码:
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
[Parse setApplicationId:appId
clientKey:clKey];
if (application.applicationState != UIApplicationStateBackground) {
BOOL preBackgroundPush = ![application respondsToSelectorselector(backgroundRefreshStatus)];
BOOL oldPushHandlerOnly = ![self respondsToSelectorselector(application:didReceiveRemoteNotification:fetchCompletionHandler];
BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
}
}
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeAlert|
UIRemoteNotificationTypeSound|
UIRemoteNotificationTypeNewsstandContentAvailability];
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
[self processPushNotification:notificationPayload foreground:YES];
return YES;
}
- (void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)deviceToken
{
TFLog(@"didRegisterForRemoteNotificationsWithDeviceToken");
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
TFLog(@"deviceToken: %@, currentInstallation.badge: %ld", currentInstallation.deviceToken, (long)currentInstallation.badge);
TFLog(@"deviceToken: %@, deviceType: %@", currentInstallation.deviceToken, currentInstallation.deviceType);
TFLog(@"installationId: %@", currentInstallation.installationId);
}
- (void)applicationUIApplication *)application didFailToRegisterForRemoteNotificationsWithErrorNSError *)error {
TFLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
if (error.code == 3010) {
TFLog(@"ush notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
TFLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
}
}
- (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo {
TFLog(@"%@", userInfo);
[PFPush handlePush:userInfo];
[self processPushNotification:userInfo foreground:YES];
[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}
- (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
TFLog(@"didReceiveRemoteNotification2");
[self processPushNotification:userInfo foreground:YES];
[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}
因此,应用在所有状态下都会收到远程通知,但未运行时除外。 我错过了什么?
您错过了 Local and Push Notification Programming Guide 中的位。它说的地方-
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification
另外,来自 Apple 的这份说明 -
Important: Delivery of notifications is a “best effort”, not guaranteed. It is not intended to deliver data to your app, only to notify the user that there is new data available.
如果您的应用程序是从应用程序图标而不是通知启动的,则您需要检查更新的内容,而与可能已收到的任何推送通知无关。这使得应用程序在从通知打开和从应用程序图标打开时的行为有所不同。
例如,Facebook 应用程序在从通知警报启动时直接打开通知中的项目,但从应用程序图标启动时不会打开 - 从用户的角度来看,这是“正确”的行为。如果我与通知交互,那么我会对它的内容感兴趣。如果我从图标启动应用程序,那么我只想使用该应用程序 - 如果需要,我可以访问应用程序中的通知。
关于ios - 应用未运行时未收到远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962873/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) | Powered by Discuz! X3.4 |