猿问

在后台时didReceiveRemoteNotification

这类问题已被问过很多次,但我有一些具体情况在进行。

当我的应用程序处于活动状态并且收到PUSH消息时,我就能够成功解析自定义有效负载等。

但是,当我的应用程序在后台并且PUSH到达时,用户必须单击“查看/打开”按钮才能被didReceiveRemoteNotification调用,然后didFinishLaunchingWithOptions被调用。

我需要让我的应用程序决定在后台时是否必须使用UIAlert提示用户或基于某些本地设置禁止显示推送消息。

任何帮助,将不胜感激,


慕的地10843
浏览 1529回答 3
3回答

MM们

您的应用需要处理所有可能的推送通知传递状态:您的应用刚刚启动您的应用只是从后台引入到前台您的应用已经在前台运行您不必在交付时选择使用哪种呈现方法来呈现推送通知,该呈现方法已编码在通知本身中(可选的警报,证件编号,声音)。但是,由于您大概可以控制应用程序和推送通知的有效负载,因此您可以在有效负载中指定是否已经向用户显示了警报视图和消息。仅在应用程序已经在前台运行的情况下,您才知道用户不仅通过警报或定期从主屏幕启动您的应用程序。您可以使用以下代码在didReceiveRemoteNotification中判断您的应用是否刚刚出现在前台:- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    if ( application.applicationState == UIApplicationStateActive )        // app was already in the foreground    else        // app was just brought from background to foreground    ...}

尚方宝剑之说

传递content-available = 1您的有效负载,didReceiveRemoteNotification甚至在后台调用。例如{    "alert" : "",    "badge" : "0",    "content-available" : "1",    "sound" : ""}

梵蒂冈之花

当应用程序在后台运行时,您必须做一些事情才能管理收到的推送通知。首先,在服务器端,您必须设置{"aps":{"content-available" : 1... / $body['aps']['content-available'] =1;推送通知有效负载。其次,在您的Xcode项目中,您必须简化“远程通知”。通过转到项目的目标->功能,然后启用功能切换并选中“远程通知”复选框来完成此操作。第三,不必使用,而didReceiveRemoteNotification必须调用 application:didReceiveRemoteNotification:fetchCompletionHandler:,这将使您能够在收到通知时在后台执行所需的任务:- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if(application.applicationState == UIApplicationStateInactive) {     NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");     //do some tasks    [self manageRemoteNotification:userInfo];     completionHandler(UIBackgroundFetchResultNewData); } else if (application.applicationState == UIApplicationStateBackground) {     NSLog(@"application Background - notification has arrived when app was in background");     NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];     if([contentAvailable isEqualToString:@"1"]) {         // do tasks         [self manageRemoteNotification:userInfo];         NSLog(@"content-available is equal to 1");         completionHandler(UIBackgroundFetchResultNewData);     } } else {     NSLog(@"application Active - notication has arrived while app was opened");        //Show an in-app banner         //do tasks        [self manageRemoteNotification:userInfo];         completionHandler(UIBackgroundFetchResultNewData);     } }最后,您必须UIRemoteNotificationTypeNewsstandContentAvailability在设置通知类型时将其添加到通知设置中。除此之外,如果在通知到达时您的应用已关闭,则您必须在中进行管理didFinishLaunchingWithOptions,并且即使用户点击了推送通知也是如此:if (launchOptions != nil){    NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];    if (dictionary != nil)    {        NSLog(@"Launched from push notification: %@", dictionary);        [self manageRemoteNotification:dictionary];    }}当您通过点击推送通知启动应用程序时,launchOptions为!= nil,如果您通过点击图标访问应用程序,launchOptions将为== nil。
随时随地看视频慕课网APP
我要回答