首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在后台使用didReceiveRemoteNotification

在后台使用didReceiveRemoteNotification
EN

Stack Overflow用户
提问于 2011-02-20 19:21:03
回答 4查看 72.5K关注 0票数 54

这类问题已经被问了很多次了,但我有一些具体的情况在继续。

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

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

我需要让我的应用程序决定是否必须在后台使用UIAlert提示用户,或者根据某些本地设置抑制推送消息。

任何帮助都会很感激,

EN

回答 4

Stack Overflow用户

发布于 2016-01-15 21:28:16

当应用程序在后台时,你必须做几件事来管理收到的推送通知。

首先,在您的服务器端,您必须在推送通知负载中设置{"aps":{"content-available" : 1... / $body['aps']['content-available'] =1;

其次,在您的Xcode项目中,您必须使“远程通知”生效。方法是转到项目的目标->功能,然后启用功能开关,并选中remote notifications复选框。

第三,您必须调用application:didReceiveRemoteNotification:fetchCompletionHandler:,而不是使用didReceiveRemoteNotification,这将允许您在收到通知的时刻在后台执行您想要的任务:

代码语言:javascript
复制
- (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中管理它,只要用户点击推送通知:这样做的方法是:

代码语言:javascript
复制
if (launchOptions != nil)
{
    NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (dictionary != nil)
    {
        NSLog(@"Launched from push notification: %@", dictionary);
        [self manageRemoteNotification:dictionary];
    }
}

launchOptions是!= nil当你通过点击推送通知启动应用程序时,如果你通过点击图标进入它,launchOptions将是== nil。

我希望这将是有用的。Here it is explained by Apple

票数 16
EN

Stack Overflow用户

发布于 2014-12-30 23:21:48

content-available = 1与您的有效负载一起传递,即使在后台也会调用didReceiveRemoteNotification。例如:

代码语言:javascript
复制
{
    "alert" : "",
    "badge" : "0",
    "content-available" : "1",
    "sound" : ""
}
票数 15
EN

Stack Overflow用户

发布于 2011-02-20 20:10:25

请记住,当您的推送消息到达用户的iPhone并单击“取消”时,除了图标徽章编号(它们将由操作系统处理)之外,您的后台应用程序将无法知道此推送事件并采取任何进一步的操作。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5056689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档