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

相关文章

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • iOS-iOS10极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • iOS-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • 极光推送集成开发

    1.极光推送集成与设置 极光推送地址①注册极光推送账号。②在应用管理内按照步骤创建APP。③找到“文档——iOS—...

  • iOS —— 极光推送和极光IM

    前言 (环境:iOS12.0、极光推送SDK3.1.0、极光IM3.7.0) 写iOS 推送(苹果原生态)时,笔者...

  • 使用苹果原生APNS和后端推送工具Easy APNs Provi

    最近研究苹果原生apns,极光推送在此不详细解释了,具体查看极光文档极光推送传送门 原生APNS,iOS 代码如下...

  • 环信消息推送

    一,关于推送 之前做过 极光 APNS 个推的 推送 : 这里说下 极光推送是比较 适合用在 iOS 端和 安卓端...

  • iOS 推送参考文档

    1、ios 消息推送证书设置和整理(备忘)2、iOS 远程推送APNS从0至发布-极光推送& 真机测试篇3、iOS...

  • IOS 推送 (极光推送)

    今天朋友说到推送,因为以前也没做过,就跟着看了看极光的推送.(自己的每一步,很详细,很啰嗦..大神就不用看了......

  • iOS开发极光推送遇到的问题

    极光推送文档:https://docs.jiguang.cn/jpush/client/iOS/ios_api/#...

网友评论

    本文标题:iOS极光推送

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