- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
#ifdef __IPHONE_8_0
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#else
//这里还是原来的代码
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
#endif
//判断是否由远程消息通知触发应用程序启动
if (launchOptions) {
//获取应用程序消息通知标记数(即小红圈中的数字)
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
if (badge>0) {
//如果应用程序消息通知标记数(即小红圈中的数字)大于0,清除标记。
badge--;
//清除标记。清除小红圈中数字,小红圈中数字为0,小红圈才会消除。
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
NSDictionary *userInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
//获取推送详情
#ifdef __IPHONE_7_0
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
#else
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:userInfo];
#endif
}
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
}
#ifdef __IPHONE_7_0
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"%@", userInfo);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:userInfo.description delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
if (completionHandler) {
if (userInfo[@"extra"]) {
completionHandler(UIBackgroundFetchResultNewData);
}else{
completionHandler(UIBackgroundFetchResultNoData);
}
}
}
#else
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"%@". userInfo);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:userInfo.description delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
}
#endif
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"%@", error);
}
iOS 推送处理
最新推荐文章于 2024-08-09 07:34:05 发布