简体   繁体   English

当app在后台时,如何对接收推送通知执行操作?

[英]how to perform action on receive push notification when app is in background?

My app using remote notification when we want to inform user that new data is available for his application ,In code I had written updateData method (which pulls data from server) in didReceiveRemoteNotification method of appDelegate. 当我们想要通知用户新数据可用于他的应用程序时,我的应用程序使用远程通知,在代码中我已经在appDelegate的didReceiveRemoteNotification方法中编写了updateData方法(从服务器提取数据)。 Now it works fine if my application is active, but not for inactive mode.Is I am wrong at somewhere ? 现在它可以正常工作,如果我的应用程序是活动的,但不适用于非活动模式。我在某个地方错了吗? what will be solution for that ? 什么是解决方案? Thanks in advance. 提前致谢。

I know of no way to handle push notifications even if your app is not in active state (And I think that's exactly the it should be) 我知道即使你的应用程序没有处于活动状态也无法处理推送通知(而且我认为这应该是应该的)

You should check for PNS in your AppDelegate's didFinishLaunch as well 您应该在AppDelegate的didFinishLaunch中检查PNS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    //...
    //If Push Notification
    NSDictionary *pnsDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(pnsDict != nil){
        DDLogInfo(@"PNS");
        [self handlePushNotification:pnsDict]; 
    }
    //..
}

This way you can be sure that you catch all remote notifications. 这样您就可以确保捕获所有远程通知。

Be aware, that your app should not depend on pns . 请注意,您的应用不应该not depend on pns PNS maybe fail to be delivered or the user can turn them off. PNS可能无法交付或用户可以将其关闭。 The application should always work with pns enabled as well as pns disabled in the same way. 应用程序应始终使用pns启用以及以相同方式禁用pns。

For more information read the Apple PNS Guide 有关更多信息,请阅读Apple PNS指南

That's how notifications are supposed to work: if the app is the foreground app, it receives the notification directly; 这就是通知应该如何工作:如果应用程序是前台应用程序,它会直接接收通知; otherwise the user sees a message pop up asking if they want to activate the app. 否则,用户会看到一条消息,询问他们是否要激活该应用。 It's described clearly in the docs . 在文档中有清楚的描述。 You can't avoid that dialog if you want to use APNS. 如果要使用APNS,则无法避免该对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM