iOS 消息推送--本地推送-网络推送

  • 本地推送

  • "AppDelegate.m"
/*
 UIUserNotificationTypeBadge   图标标记   001
 UIUserNotificationTypeSound   声音      010
 UIUserNotificationTypeAlert   弹框      100
 111
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 注册推送通知
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert  categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    
    // 当程序被杀死情况下接收本地推送通知
    UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 220, 200)];
        label.backgroundColor = [UIColor redColor];
        label.text = [NSString stringWithFormat:@"%@",localNotification.userInfo];
        label.numberOfLines = 0;
        [self.window.rootViewController.view addSubview:label];
    }
    return YES;
}

/// 获取本地推送通知携带的信息
/// 在前台也会调用以下方法
/// 程序被杀死不会调用以下方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"%@",notification.userInfo);
    // 控制台调试(前提条件 :程序需要和xcode连接)
    // UI调试
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 220, 200)];
    label.backgroundColor = [UIColor redColor];
    [self.window.rootViewController.view addSubview:label];
}

  • ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 设置0去隐藏图标标记
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 创建本地推送通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    // 通知触发时间
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 推送通知的内容
    localNotification.alertBody = @"xxx: xxxxx.";
    // 推送的声音
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    // 应用图标标记   0 代表不改变原来数字    负值可以销毁图标标记
    localNotification.applicationIconBadgeNumber = 10;
    // 修改通知中心的名称
//    localNotification.alertTitle = @"嘻嘻嘻";
//    // 修改锁屏时'滑动来' 后面的文字
//    localNotification.alertAction = @"哈哈哈";
    // 通知携带的具体信息   让程序员使用
    localNotification.userInfo = @{@"content":@"wokaiwanxiaode",@"name":@"yadan"};
    // 将通知加入调度池
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
  • 远程推送

  • AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 注册推送通知
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    // 向苹果请求deviceToken   将UDID + BundleID  发送给苹果
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    
    // 程序被杀死情况下获取远程推送信息
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
        label.text = [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
        label.numberOfLines = 0;
        [self.window.rootViewController.view addSubview:label];
    }
    return YES;
}

// 苹果返回deviceToken时调用
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // 05859cfd 54157d56 a7ea2046 2087d0e9 f96ae9ca fbd73911 dc097c08 9a936879
    NSLog(@"%@",deviceToken);
    // 发送给你们公司的服务器
}

/// 接收远程推送信息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"%@",userInfo);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    label.text = [NSString stringWithFormat:@"%@",userInfo];
    label.numberOfLines = 0;
    [self.window.rootViewController.view addSubview:label];
}

  • 远程推送, 示意图


    iOS 消息推送--本地推送-网络推送_第1张图片
    远程推送deviceToken.png

你可能感兴趣的:(iOS 消息推送--本地推送-网络推送)