简体   繁体   English

当应用关闭时,如何从推送通知中获取数据?

[英]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.

相关问题 iOS:确定用户已关闭来自APNS提供商的推送通知 - iOS: Identify User turned off Push Notification from APNS provider 当应用关闭时,将通知或意图从BLE设备推送到电话 - Push Notifications or Intents to Phone from BLE Device when App is turned off iOS5:如何确定应用程序的通知中心是否打开/关闭 - iOS5: how to determine if the Notification Center for the app is turned ON/OFF 关闭“推送通知”会怎样? 是否将执行代码(减去UI)? - What happens when Push Notification is turned off ? Will code be executed (minus UI)? 在iPhone打开或关闭时发送通知 - Send notification when iPhone is turned on or off 当用户使用 Firebase FCM swizzling 关闭 iOS 设备设置中的通知时,如何实现应用行为? - How to implement app behavior when user turn off notification in device setting for iOS using Firebase FCM swizzling turned on? 如何在 Swift5 中关闭应用程序时更改应用程序状态? - How to change the app status when the app is turned off in Swift5? 如何判断推送通知是否尚未注册或已关闭? - How can I tell whether push notification is not registered yet or has been turned off? IOS:当蜂窝数据打开且 Wi-Fi 关闭时,如何启用 react-native 提取工作? - IOS: How do I enable react-native fetch to work when Cellular Data is turned on and Wi-Fi is off? 如何判断用户是否为我的应用关闭了蜂窝数据? - How to tell if the user turned off cellular data for my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM