美文网首页
极光推送

极光推送

作者: 精神薇 | 来源:发表于2019-02-21 14:30 被阅读0次
#pragma mark - 判断是不是发布环境
#ifdef DEBUG
#define isProduct FALSE
#else
#define isProduct TRUE
#endif

.h文件如下:

static NSString *appKey = @"xxxxxxxxxxxxxxxx";
static NSString *channel = @"App Store";
static BOOL isProduction = isProduct;//0(默认值)表示采用的是开发证书,1 表示采用生产证书发布应用
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

.m文件如下:

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

服从<JPUSHRegisterDelegate>,然后在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions文件中如下:

//jpush
    [self jPush:launchOptions];
    
    //iOS 10以下程序在关闭状态收到通知,只有点击才能显示
    if(CurrentDeviceVersion<10.0){
        [self showNotificationsAPNS:launchOptions];
    }

实现方法如下:

#pragma mark 极光推送
- (void)jPush:(NSDictionary *)launchOptions{
    // 3.0.0及以后版本注册
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    if (@available(iOS 12.0, *)) {
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
    } else {
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        //    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //      NSSet<UNNotificationCategory *> *categories;
        //      entity.categories = categories;
        //    }
        //    else {
        //      NSSet<UIUserNotificationCategory *> *categories;
        //      entity.categories = categories;
        //    }
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    //如不需要使用IDFA,advertisingIdentifier 可为nil
    [JPUSHService setupWithOption:launchOptions appKey:appKey
                          channel:channel
                 apsForProduction:isProduction
            advertisingIdentifier:nil];
    
    //2.1.9版本新增获取registration id block接口。
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功:%@",registrationID);
            
        }
        else{
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];
}

#pragma mark iOS 10以下程序在关闭状态收到通知,只有点击才能显示
- (void)showNotificationsAPNS:(NSDictionary *)launchOptions{
    NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    if (pushInfo)
    {
        NSDictionary *apsInfo = [pushInfo objectForKey:@"aps"];
        if(apsInfo)
        {
            //推送内容展示
            NSString *subtitle = [apsInfo objectForKey:@"alert"];  // 推送的消息
            UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
                                                           message:subtitle
                                                          delegate:self
                                                 cancelButtonTitle:@"好的"
                                                 otherButtonTitles:nil, nil];
            [test show];
        }
        
    }
}

最后在AppDelegate.m文件中的代理方法下面添加JPush的代理方法,如下:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [application setApplicationIconBadgeNumber:0];   //清除角标
    [JPUSHService setBadge:0];//同样的告诉极光角标为0了
    [application cancelAllLocalNotifications];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [application setApplicationIconBadgeNumber:0];   //清除角标
    [JPUSHService setBadge:0];//同样的告诉极光角标为0了
    [application cancelAllLocalNotifications];
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"iOS7及以上系统,收到通知");
    //推送内容展示
    NSString *subtitle = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];  // 推送的消息
    UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
                                                   message:subtitle
                                                  delegate:self
                                         cancelButtonTitle: @"好的"
                                         otherButtonTitles:nil, nil];
    [test show];
    
    completionHandler(UIBackgroundFetchResultNewData);
}
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#pragma mark- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler API_AVAILABLE(ios(10.0)){
    NSDictionary * userInfo = notification.request.content.userInfo;
    
    UNNotificationRequest *request = notification.request; // 收到推送的请求
    UNNotificationContent *content = request.content; // 收到推送的消息内容
    
    NSNumber *badge = content.badge;  // 推送消息的角标
    NSString *body = content.body;    // 推送消息体
    UNNotificationSound *sound = content.sound;  // 推送消息的声音
    NSString *subtitle = content.subtitle;  // 推送消息的副标题
    NSString *title = content.title;  // 推送消息的标题
    
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        NSLog(@"iOS10 前台收到远程通知");
        
    }
    else {
        // 判断为本地通知
        NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
    }
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    UNNotificationRequest *request = response.notification.request; // 收到推送的请求
    UNNotificationContent *content = request.content; // 收到推送的消息内容
    
    NSNumber *badge = content.badge;  // 推送消息的角标
    NSString *body = content.body;    // 推送消息体
    UNNotificationSound *sound = content.sound;  // 推送消息的声音
    NSString *subtitle = content.subtitle;  // 推送消息的副标题
    NSString *title = content.title;  // 推送消息的标题
    
    
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        NSLog(@"iOS10 收到远程通知");
        //推送内容展示
        UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
                                                       message:body
                                                      delegate:self
                                             cancelButtonTitle:@"好的"
                                             otherButtonTitles:nil, nil];
        [test show];
        
    }
    else {
        // 判断为本地通知
        NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
    }
    
    completionHandler();  // 系统要求执行这个方法
}
#endif

#ifdef __IPHONE_12_0
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification API_AVAILABLE(ios(10.0)){
    if (notification) {
        //从通知界面关闭-配置直接进入应用
        NSDictionary * userInfo = notification.request.content.userInfo;
        UNNotificationRequest *request = notification.request; // 收到推送的请求
        UNNotificationContent *content = request.content; // 收到推送的消息内容
        NSString *body = content.body;    // 推送消息体
        //推送内容展示
        UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
                                                       message:body
                                                      delegate:self
                                             cancelButtonTitle:@"好的"
                                             otherButtonTitles:nil, nil];
        [test show];
    }else{
        //从通知界面-设置界面进入应用
    }
    
    
}
#endif

相关文章

  • 极光推送

    极光推送视频地址,非常详细的极光推送视频 极光推送

  • 极光推送

    极光推送 tagprivate void initJpush() {//TODO 极光推送// JPushInte...

  • 极光推送进行远程推送

    借阅:极光推送进行远程推送 怎么使用极光推送进行远程推送 在极光官网注册极光推送创建一个应用在应用配置中导入两个证...

  • ios极光推送

    第一次使用极光推送,在这里把极光推送的步骤说一下,省的以后再次用到极光推送的时候,给忘了,其实,极光推送不难...

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

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

  • 2018年功能模块沉淀

    一、推送模块 1.极光推送 文档:https://www.jiguang.cn/push备注:极光推送包括普通推送...

  • 极光推送(二)——推送的使用

    前言 在极光推送(一)——配置中讲过了极光推送的配置,这节讲讲极光推送的使用参考文档极光官网 下面以我写的demo...

  • 极光推送集成开发

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

  • Flutter开发 集成极光推送

    Flutter推送 极光推送Flutter版本 最近研究Flutter推送,在网上找了很多资料,发现极光推送竟然有...

  • 极光征文 | 我与极光的缘分

    极光征文 | 我与极光的缘分 首先简单介绍下极光推送;极光推送(JPush)是独立的第三方云推送平台,致力于为全球...

网友评论

      本文标题:极光推送

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