3 回答
TA贡献1886条经验 获得超2个赞
您的应用需要处理所有可能的推送通知传递状态:
您的应用刚刚启动
您的应用只是从后台引入到前台
您的应用已经在前台运行
您不必在交付时选择使用哪种呈现方法来呈现推送通知,该呈现方法已编码在通知本身中(可选的警报,证件编号,声音)。但是,由于您大概可以控制应用程序和推送通知的有效负载,因此您可以在有效负载中指定是否已经向用户显示了警报视图和消息。仅在应用程序已经在前台运行的情况下,您才知道用户不仅通过警报或定期从主屏幕启动您的应用程序。
您可以使用以下代码在didReceiveRemoteNotification中判断您的应用是否刚刚出现在前台:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateActive )
// app was already in the foreground
else
// app was just brought from background to foreground
...
}
TA贡献1788条经验 获得超4个赞
传递content-available = 1您的有效负载,didReceiveRemoteNotification甚至在后台调用。例如
{
"alert" : "",
"badge" : "0",
"content-available" : "1",
"sound" : ""
}
TA贡献1900条经验 获得超5个赞
当应用程序在后台运行时,您必须做一些事情才能管理收到的推送通知。
首先,在服务器端,您必须设置{"aps":{"content-available" : 1... / $body['aps']['content-available'] =1;推送通知有效负载。
其次,在您的Xcode项目中,您必须简化“远程通知”。通过转到项目的目标->功能,然后启用功能切换并选中“远程通知”复选框来完成此操作。
第三,不必使用,而didReceiveRemoteNotification必须调用 application:didReceiveRemoteNotification:fetchCompletionHandler:,这将使您能够在收到通知时在后台执行所需的任务:
- (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,并且即使用户点击了推送通知也是如此:
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
[self manageRemoteNotification:dictionary];
}
}
当您通过点击推送通知启动应用程序时,launchOptions为!= nil,如果您通过点击图标访问应用程序,launchOptions将为== nil。
- 3 回答
- 0 关注
- 1528 浏览
添加回答
举报