[英]How to fetch data from push notification when app is turned off?
I want to fetch data from push notification when app is turned off. 我想在应用程序关闭时从推送通知中获取数据。 I am using the following code given below: 我正在使用下面给出的以下代码:
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSDictionary *aps = userInfo[@”aps”];
if (state == UIApplicationStateInactive){
// user tapped notification
NSLog(@"results%@",aps);
completionHandler(UIBackgroundFetchResultNewData);
} else {
// app is active
completionHandler(UIBackgroundFetchResultNoData);
}
}
In above code NSLog(@"results%@",aps); 在上面的代码NSLog(@“ results%@”,aps); doesn't get print the values when app is in background. 当应用程序在后台运行时,不会获得打印值。 Please Help me out 请帮帮我
didReceiveRemoteNotification works only when you tap on notification. didReceiveRemoteNotification仅在您点击通知时起作用。 so it will provide you data only when you tap on notification and your app will get open. 因此只有在您点击通知时,它才会为您提供数据,并且您的应用程序将打开。
If you want to get NSDictionary *aps = userInfo[@”aps”]; 如果要获取NSDictionary * aps = userInfo [@” aps”]; when your app is in kill state, then you have to implement pushkit framework. 当您的应用程序处于kill状态时,则必须实现pushkit框架。
You can download sample code from 1 my answer 您可以从1我的答案中下载示例代码
Can someone share a sample code of iOS Xamarin PushKit in C#? 有人可以在C#中共享iOS Xamarin PushKit的示例代码吗?
Register for voip notifications outside of app delegate 在应用程序委托外注册VOIP通知
Source https://github.com/hasyapanchasara/PushKit_SilentPushNotification 来源 https://github.com/hasyapanchasara/PushKit_SilentPushNotification
When is app is turned off or terminated to be precise , didReceiveRemoteNotification
is not called for obvious reasons Appdelegate's didFinishLaunchingWithOptions
launchOptions provides a solution for this. 当应用程序被关闭或终止准确地说, didReceiveRemoteNotification
不叫出于显而易见的原因的appDelegate的didFinishLaunchingWithOptions
launchOptions为此提供了一个解决方案。 The Launchoptions is basically a dictionary to tell the app in which way it was launched.For your implementation you need to check if launchOptions has any value in the key UIApplicationLaunchOptionsRemoteNotificationKey
, like so () Launchoptions基本上是一个字典,用于告诉应用程序以哪种方式启动。对于您的实现,您需要检查launchOptions是否在键UIApplicationLaunchOptionsRemoteNotificationKey
具有任何值,例如()
let userInfo = launchOptionForApp[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
your userInfo dictionary has the json you will need to parse and implement your further logic 您的userInfo字典具有您需要解析并实现进一步逻辑的json
Sample change from server end that done the trick for me. 服务器端的示例更改为我完成了窍门。
{
aps = {
"content-available" : 1,
sound : ""
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.