美文网首页
iOS10 消息推送 代码

iOS10 消息推送 代码

作者: okerivy | 来源:发表于2017-06-24 22:52 被阅读18次

0, 配置push证书 省略

1, 设置Xcode

打开开关


2, 导入头文件 注册通知

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self listenNetWorkingStatus]; //监听网络是否可用

    
    if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
        
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        
        center.delegate = self;
        
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if (!error) {
                
                NSLog(@"request authorization succeeded!");
                
            }
            
        }];
        
    } else {
        
        if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
            
            //IOS8,创建UIUserNotificationSettings,并设置消息的显示类类型
            
            UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
            
            [application registerUserNotificationSettings:notiSettings];
            
        }
        
    }
    
     [[UIApplication sharedApplication] registerForRemoteNotifications];

    [self initUIAppearance];

    
    // 1.创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    // 2.设置根控制器
    self.window.rootViewController = [[DATabBarController alloc] init];

    // 3.显示窗口
    [self.window makeKeyAndVisible];

    return YES;
}


3, 实现代理方法


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
    
    NSLog(@"deviceToken=====%@",deviceToken);
    
    NSString*device = [[[[deviceToken description]stringByReplacingOccurrencesOfString:@"<"withString:@""]stringByReplacingOccurrencesOfString:@" "withString:@""]stringByReplacingOccurrencesOfString:@">"withString:@""];
    
    NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:device forKey:@"DEVICE_TOKEN"];
    
    [defaults synchronize];
    
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

{
    
    NSLog(@"%@",userInfo);
    
    application.applicationIconBadgeNumber = 0;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivePush" object:nil userInfo:userInfo];
    
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
    
    NSLog(@"Regist fail%@",error);
    
}


4, 测试通知

用这个软件测试推送
https://github.com/noodlewerk/NWPusher

手机运行到后台 就能看到通知的

5, 收到消息后, 图标显示数字

当你挂起程序时,你收到了一个远程通知,如果你的服务器发送通知时,通知的内容设置了badge = 10吧,那么你不用打开应用,然后解析badge,得到10,再把3设置为icon的badgenumber = 10

收到通知的那一刻,你的icon badge number就已经显示为10了

相关文章

  • iOS10 消息推送 代码

    0, 配置push证书 省略 1, 设置Xcode 打开开关 2, 导入头文件 注册通知 3, 实现代理方法 4,...

  • iOS10推送必看UNNotificationContentEx

    iOS10推送必看UNNotificationContentExtension iOS10推送必看UNNotifi...

  • iOS10推送必看UNNotificationServiceEx

    iOS10推送必看UNNotificationServiceExtension iOS10推送必看UNNotifi...

  • iOS10推送开发

    iOS10推送授权 UNNotificationSettings属性 iOS10本地推送示例 定义推送 以上就是一...

  • iOS JPush初探

    前言 最近iOS10的更新,客户反映收不到推送消息,经测试发现是在iOS10的上,JPush不回调之前设置好的响应...

  • iOS 推送总结

    注册推送 收到推送 iOS10以前 iOS10之后 关于推送的声音播放 苹果默认的推送声音就不说了,需要播放自定义...

  • iOS 10极光推送适配(最新)

    iOS10发布后,发现项目中的极光推送接收消息异常了。查了相关资料后才发现,iOS10中对于通知做了不少改变。同时...

  • 友盟推送坑点(测试生产环境推送)

    背景:iOS10升级后,系统xcode未升级,集成时没有集成iOS10的推送导致演示项目iOS10无法收到推送 1...

  • iOS10推送通知整理总结

    这篇文章整理iOS10之后的推送通知(文中的推送通知,如不做特殊说明,默认是iOS10以后的推送通知) iOS10...

  • iOS推送通知学习与总结

    这篇文章整理iOS10之前的推送通知(文中的推送通知,如不做特殊说明,默认是iOS10以前的推送通知) iOS10...

网友评论

      本文标题:iOS10 消息推送 代码

      本文链接:https://www.haomeiwen.com/subject/tqqlcxtx.html