iOS极光推送

作者: dpplh | 来源:发表于2017-12-07 20:26 被阅读255次

    推送

    1. 导入极光推送sdk

    通过cocoaPod导入:

    • 通过Cocoapods下载地址:
    pod 'JPush'
    
    • 如果需要安装指定版本则使用以下方式(以3.0.2版本为例):
    pod 'JPush', '3.0.2'
    

    2. 开启xcode中的推送设置

    打开设置后生成.entitlements文件

    image

    3. AppDelegate中注册推送

    • 导入头文件
    // 引入JPush功能所需头文件
    #import "JPUSHService.h"
    // iOS10注册APNs所需头文件
    #ifdef NSFoundationVersionNumber_iOS_9_x_Max
    #import <UserNotifications/UserNotifications.h>
    #endif
    
    • 注册代码

    apsForProduction: YES - 生产环境 NO - 开发环境
    需要对应相应证书

    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        
        [JPUSHService setupWithOption:launchOptions appKey:JPUSH_APP_KEY channel:@"App Store" apsForProduction:NO];
        
    }
    
    
    • 注册通知代码回调
    // 注册成功回调
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        // Required - 注册 DeviceToken
        
        [JPUSHService registerDeviceToken:deviceToken];
    }
    
    // 注册失败回调
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }
    
    
    • iOS10及以上版本在前台收到推送
    // iOS 10 Support
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
        // Required
        NSDictionary * userInfo = notification.request.content.userInfo;
        if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
        completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
    }
    
    • 点击推送方法调用

    可以在回调中处理跳转到界面等逻辑

    // iOS 10 Support
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
        // Required
        NSDictionary * userInfo = response.notification.request.content.userInfo;
        if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
        completionHandler();  // 系统要求执行这个方法
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        // Required, iOS 7 Support
        [JPUSHService handleRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        // Required,For systems with less than or equal to iOS6
        [JPUSHService handleRemoteNotification:userInfo];
    }
    

    4.极光测试推送

    • 步骤如下:


      image
      image
      image
    • 推送内容


      image
    • 后台推送格式

     "ios": {
                "alert": {
                    "title": "推送标题",
                    "subtitle": "推送副标题",
                    "body": "推送内容"
                }
                "sound": "default",
                "badge": "+1",
                "extras": {} // 附加参数
            }
    

    后台需要严格遵循以上格式,否则需要在前端将推送内容转为以上格式

    自定义消息

    初始化自定义消息

    需要在打开App的情况下才会接收到自定义消息

    • 监听自定义消息
    // 注册自定义推送
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];    
    }
    
    //在APP打开的情况下,接收自定义的方法
    - (void)networkDidReceiveMessage:(NSNotification *)notification {
        //处理通知的方法
        NSDictionary *userInfo = [notification userInfo];
    }
        
    

    测试自定义消息

    • 步骤如下


      image
      image

    相关文章

      网友评论

        本文标题:iOS极光推送

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