推送消息机制

2019-02-28  本文已影响0人  陌巷先森

一、简介

iOS推送机制分为本地通知(Local Notifications)和远程推送(Remote Notifications)两种。可以在应用没有打开甚至手机锁屏的情况下给用户提示。它们都需要进行注册,用户同意后才可以正常使用,如果用户不同意则下次不弹出提示框,需要用户手动设置。iOS10后将之前的推送通知统一成UserNotifications.framework来集中管理和使用通知功能。

二、推送机制

Local Notifications(本地通知)

Local Notifications

本地推送就是通过App本地创建通知,加入到系统的Schedule里,如果触发器条件达成时会推送相应的消息内容

Remote Notifications(远程推送)

Remote Notifications
Provider:就是为指定iOS设备应用程序提供Push的服务器。如果iOS设备的应用程序是客户端的话,那么Provider可以理解为服务端(推送消息的发起者)
APNs:Apple Push Notification Service(苹果消息推送服务器)
Devices:iOS设备,用来接收APNs下发下来的消息
Client App:iOS设备上的应用程序,用来接收APNs下发的消息到指定的一个客户端app(消息的最终响应者)

基本配置和方法

import

导入 #import <UserNotifications/UserNotifications.h>
且要遵守<UNUserNotificationCenterDelegate>的协议

注册推送


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中注册通知

if (@available(iOS 10.0, *)) {
    //iOS10之后的注册方法
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate=self;
    UNAuthorizationOptions types=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:types     completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //点击允许
            //这里可以添加一些自己的逻辑
        } else {
            //点击不允许
            //这里可以添加一些自己的逻辑
        }
    }];
    }else{
        //iOS8~iOS10的注册方法
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:settings];
    }
//注册远端消息通知获取device token
[application registerForRemoteNotifications];

获取device token

远程推送需要获取设备的device token

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                                 stringByReplacingOccurrencesOfString: @">" withString: @""]
                                stringByReplacingOccurrencesOfString: @" " withString: @""];
    NSLog(@"my device token---%@",deviceTokenStr);
}

处理推送消息

iOS 10系统更新时,苹果给了我们2个代理方法来处理通知的接收和点击事件,这两个方法在<UNUserNotificationCenterDelegate>的协议中

//下面这个代理方法,只会是app处于前台状态才会走,后台模式下是不会走这里的
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
//下面这个代理方法,只会是用户点击消息才会触发,如果使用户长按(3DTouch)、弹出Action页面等并不会触发。点击Action的时候会触发!
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler

//点击代理最后都需要执行:completionHandler(),前台时需要在前台代理方法中进行如下设置才能完整的展示出通知
completionHandler(UNNotificationPresentationOptionBadge|
                  UNNotificationPresentationOptionSound|
                  UNNotificationPresentationOptionAlert);

三、本地通知

APP本地通知的主要流程是:
1.创建一个触发器(trigger)
2.创建通知的内容(UNMutableNotificationContent)
3.创建通知请求(UNNotificationRequest)
4.通知请求添加到推送管理中心(UNUserNotificationCenter)中

四、远程推送

1.推送流程:

一般情况下如果一个程序退到后台就不能运行代码,或者程序退出后就和对应应用的后台服务器断开链接,收不到服务器发送的信息,但是每台设备在联网状态下仍然会和苹果的APNs服务器建立一个长连接,通过苹果的APNs服务器,后台就可以和设备保持连接。
客户端向苹果服务器APNs发送设备的UDID和应用的Bundle Identifier,经苹果服务器加密生成一个deviceToken,将当前用户的deviceToken发送给自己应用的服务器,自己的服务器根据deviceToken进行推送消息。
当需要推送是,将消息和deviceToken一起发送给APNs,APNs再通过deviceToken找到用户,并将消息发送给用户。

参考:完整项目资料下载

上一篇 下一篇

猜你喜欢

热点阅读