苹果的沉默推送通知可以启动我的应用程序在后台? [英] Can Apple's silent push notifications launch my app in the background?

查看:100
本文介绍了苹果的沉默推送通知可以启动我的应用程序在后台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据苹果的文档,我可以通过添加内容提供= 1 键 - 值 APS 的有效载荷字典。我想我的应用程序醒来在后台时无声的通知到达。我设置响应推送通知应用软件下载量所需的背景模式在我的info.plist

According to Apple's documentation I can register for silent notification by adding "content-available" = 1 key-value in aps payload dictionary. I want my app to wake up in background when a silent notification arrives. I set App downloads content in response to push notifications value to Required background modes in my info.plist

这是我的字典载荷

{"aps":
      {
       "alert":"Notification alert","badge":1,"sound":"default","content-available":1
      }
}

我收到回调在 - (无效)应用:(*的UIApplication)的应用didReceiveRemoteNotification:(NSDictionary的*)USERINFO fetchCompletionHandler:(无效(^)(UIBackgroundFetchResult))completionHandler
,而我的应用程序是在后台。但我的问题是,我们可以得到回调,这或任何其他方法,当我们的应用程序处于灭活状态?

I am getting callbacks in -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler while my app is in background. But my question is can we get callback to this or any other method when our application is in killed state?

我不希望我的应用程序用户看到的通知,但我想我的应用程序通过无声的通知,将其唤醒在后台执行某些任务。

I don't want my app user to see the notification but I want my app to perform certain task by waking it up in background through silent notifications.

任何建议将是AP preciated。

Any suggestions would be appreciated.

推荐答案

我也遇到类似的那种情景,我写了这个code调试,如果我的应用程序正在苏醒静音通知与否。

I have faced a similar kind of scenario, I wrote this code to debug if my application is waking up on silent notification or not.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *str = [defaults objectForKey:@"key"];
    if (str  == nil) {
        str = @"didReceiveRemoteNotification";
    }else{
        str = [NSString stringWithFormat:@"%@, didReceiveRemoteNotification",str];
    }
    [defaults setObject:str forKey:@"key"];
    [defaults synchronize];
}

这code的工作原理是,如果你的应用程序醒来比你在这个方法中得到回调方法名将于 NSUserDefaults的被写入。因此,当您调试应用程序手动你可以看到 STR 字符串变量的值,如果有字符串 didReceiveRemoteNotification 比你会去了解你的应用程序已经醒了。

This code works like, if you app wakes up than you'll get callback in this method and the method name will be written in NSUserDefaults. So when you debug your app manually you can see the value of str string variable, if there is string didReceiveRemoteNotification than you'll get to know that you application has woke up.

请注意:这只在后台模式我。我得到的价值时,我并没有强制关闭(手动终止),我的应用程序,但是当我强迫从应用切换器关闭我的应用我不会得到任何价值。

Note: This works only in background mode for me. I get value when I did not force close(terminate manually) my app but when I force close my app from app switcher I'll not get any value.

我希望作品。

这篇关于苹果的沉默推送通知可以启动我的应用程序在后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆