本地通知和远程推送

来源:互联网 发布:查开放房记录软件 编辑:程序博客网 时间:2024/06/05 08:57

一、本地通知

1、创建通知

代码如下
- (IBAction)fireLocalNote:(id)sender {    // 1.创建本地通知    UILocalNotification *localNote = [[UILocalNotification alloc] init];    // 2.设置本地通知的内容    // 2.1.设置通知发出的时间    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];    // 2.2.设置通知的内容    localNote.alertBody = @"吃饭了吗?";    // 2.3.设置滑块的文字    localNote.alertAction = @"快点";    // 2.4.决定alertAction是否生效    localNote.hasAction = NO;    // 2.5.设置点击通知的启动图片    localNote.alertLaunchImage = @"3213432dasf";    // 2.6.设置alertTitle    localNote.alertTitle = @"3333333333";    // 2.7.设置有通知时的音效    localNote.soundName = @"buyao.wav";    // 2.8.设置应用程序图标右上角的数字    localNote.applicationIconBadgeNumber = 999;    // 2.9.设置额外信息    localNote.userInfo = @{@"type" : @1};    // 3.调用通知    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];}

2、接受通知

(1)应用之前没有被打开(该应用未在后台运行)

此时点击通知消息,进入应用,执行:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // 设置应用程序的图标右上角的数字    [application setApplicationIconBadgeNumber:0];    // iOS8.0以上需要询问是否接受通知    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];        [application registerUserNotificationSettings:settings];    }    // 界面的跳转(针对应用程序被杀死的状态下的跳转)    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {        // 跳转代码        UILabel *redView = [[UILabel alloc] init];        redView.frame = CGRectMake(0, 0, 200, 300);        redView.numberOfLines = 0;        redView.font = [UIFont systemFontOfSize:12.0];        redView.backgroundColor = [UIColor redColor];        redView.text = [NSString stringWithFormat:@"%@", launchOptions];        [self.window.rootViewController.view addSubview:redView];    }    return YES;}

(2)应用被打开了(应用在后台运行)

点击通知消息,会执行:

/* 应用程序在进入前台,或者在前台的时候都会执行该方法 */- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    // 针对应用程序在后台的时候进行的跳转    if (application.applicationState == UIApplicationStateInactive) {        NSLog(@"进行界面的跳转");        NSLog(@"%@", notification.userInfo);        UIView *redView = [[UIView alloc] init];        redView.frame = CGRectMake(0, 0, 100, 100);        redView.backgroundColor = [UIColor redColor];        [self.window.rootViewController.view addSubview:redView];    }}

二、远程推送

具体步骤:
1、从苹果服务器获取DeviceToken;
2、将DeviceToken发送给服务器,如果是用的第三方推送,如极光推送,就需要将DeviceToken发给极光推送后台

前期准备

1. 创建支持远程推送功能的App ID2. 申请开发者证书,并选中刚刚创建的App ID3. 下载CER文件,并导入钥匙串管理4. 申请发布证书,并选中刚刚创建的App ID5. 下载CER文件,并导入钥匙串管理6. 检查App ID,确认证书已经指定

步骤
1.iOS7和iOS8分别发送请求,请求DeviceToken
2.获取DeviceToken,代理 法
3.将DeviceToken发送给服务器(激光推送),pushMeBaby
4.监听远程通知的点击

1、推送信息必须的格式

格式:{"aps":{"alert":"推送需要显示的文字","badge":1,"sound":"default"}}
badge是应用图标的数字,sound通知时提示的声音,后边依照这样的字典格式,还可以拼接其他自己需要的参数信息。

2、获取DeviceToken,并且发送给服务器

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{    // 将DeviceToken传给服务器    NSLog(@"%@", deviceToken.description);}

3、注册通知

相关代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { //iOS8        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];        [application registerUserNotificationSettings:settings];        [application registerForRemoteNotifications];    } else { // iOS7        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert];    }    return YES;}

4、收到推送后,监听点击推送消息(三种情况)

(1)应用未在后台运行点击通知时进入应用进行相关操作

相关代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { //iOS8        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];        [application registerUserNotificationSettings:settings];        [application registerForRemoteNotifications];    } else { // iOS7        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert];    }    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {        // 跳转        // 添加一个红色的View    }    return YES;}

(2)应用在后台运行时,点击推送消息进入应用执行:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    NSLog(@"%@", userInfo);    // 添加一个红色的View}

(3)收到推送,直接让在后台运行的该应用执行操作

步骤
1.打开后台模式
2.告诉系统是否有新内容的更新
3.发送的通知有固定的格式(“content-available”:”1”)

1.打开后台模式

这里写图片描述

2.告诉系统是否有新内容的更新
completionHandler(UIBackgroundFetchResultNewData);

3.发送的通知有固定的格式("content-available":"1")

{“aps”:{“alert”:”推送需要显示的文字”,”badge”:1,”sound”:”default”,”content-available”:”随便填一个字符串即可”}}

代码如下:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{    NSLog(@"11111111");    UIView *redView = [[UIView alloc] init];    redView.backgroundColor = [UIColor redColor];    redView.frame = CGRectMake(100, 100, 100, 100);    [self.window.rootViewController.view addSubview:redView];    // 1.打开后台模式 2.告诉系统是否有新内容的更新 3.发送的通知有固定的格式("content-available":"1")    completionHandler(UIBackgroundFetchResultNewData);}

转载请注明出处,万分感谢!