美文网首页
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 消息推送 代码

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