gpt4 book ai didi

ios - App未运行时收不到远程通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:29 28 4
gpt4 key购买 nike

我的应用在未运行时收不到推送通知。我正在尝试处理作为 JSON 发送的远程通知,并使用来自给定 JSON 的数据更新我的应用程序中的数据。当应用程序处于事件状态或处于后台时,一切进展顺利。但是当应用程序未运行时,应用程序仅在我通过点击通知打开我的应用程序时处理通知,而不是当我通过点击图标打开应用程序时处理通知。这是来自 appDelegate 类的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId:appId
clientKey:clKey];

if (application.applicationState != UIApplicationStateBackground) {

BOOL preBackgroundPush = ![application respondsToSelector:@selector(backgroundRefreshStatus)];
BOOL oldPushHandlerOnly = ![self respondsToSelector:@selector(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)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)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)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
TFLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
if (error.code == 3010) {
TFLog(@"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
TFLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
}
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
TFLog(@"%@", userInfo);
[PFPush handlePush:userInfo];
[self processPushNotification:userInfo foreground:YES];
[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)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 - App未运行时收不到远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com