nrj*_*nrj 6 push-notification uiapplicationdelegate ios
根据苹果的文档,以找出是否一个用户轻敲你的推送通知你应该检查applicationState在application:didReceiveRemoteNotification:
如果值为UIApplicationStateInactive,则用户点击操作按钮; 如果值为UIApplicationStateActive,则应用程序在收到通知时位于最前端.
我发现这并非总是如此.例如:
双击主页按钮以显示系统托盘并进入"快速应用程序切换模式",您的应用程序向上滑动以显示其他正在运行的应用程序,您的应用程序将进入非活动状态(即使它仍然可以看到mostyle).如果您在此模式下收到推送通知,您的应用代表仍会收到,application:didReceiveRemoteNotification:并且此时您的applicationState是UIApplicationStateActive.根据文档,您应该像用户点击警报一样对待它...但在这种情况下他们没有.不仅如此,用户甚至没有看到推送通知(可能是因为在此模式下应用程序的顶部被切断).
有没有人知道检测处于"快速应用切换模式"或正确处理通知的方法?
小智 4
我可以通过一些漂亮的检查自己修复它......
本质上,整个事情的关键是
-(void)applicationDidEnterBackground:(UIApplication *)application;
Run Code Online (Sandbox Code Playgroud)
当您进入快速应用切换(或控制中心)时,不会调用此方法,因此您需要基于它设置检查。
@property BOOL isInBackground;
@property (nonatomic, retain) NSMutableArray *queuedNotifications;
Run Code Online (Sandbox Code Playgroud)
当您收到通知时...
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState appState = application.applicationState;
// Check if we're in this special state. If so, queue the message up
if (appState == UIApplicationStateInactive && !self.isInBackground) {
// This is a special case in which we're in fast app switching or control center
if (!self.queuedNotifications) {
self.queuedNotifications = [NSMutableArray array];
}
// Queue this to show when we come back
[self.queuedNotifications addObject:userInfo];
}
}
Run Code Online (Sandbox Code Playgroud)
然后当我们回来时...
- (void)applicationDidBecomeActive:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
if (!self.isInBackground) {
// Show your notifications here
// Then make sure to reset your array of queued notifications
self.queuedNotifications = [NSMutableArray array];
}
}
Run Code Online (Sandbox Code Playgroud)
您可能想做的另一件事是检查这种特殊情况:快速应用程序切换和用户去其他地方。我在设置 isInBackground BOOL 之前执行此操作。我选择将它们作为本地通知发送
-(void)applicationDidEnterBackground:(UIApplication *)application {
for (NSDictionary *eachNotification in self.queuedNotifications) {
UILocalNotification *notification = [self convertUserInfoToLocalNotification:eachNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
self.queuedNotifications = [NSMutableArray array];
self.isInBackground = YES;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1882 次 |
| 最近记录: |