iOS-JPush 3.0 版本相关问题<Background modes>

本文详细介绍了如何在iOS应用中集成JPush推送服务,包括安装配置步骤、注册推送、适配iOS10的方法以及处理推送点击事件等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面:
请先使用pod或者官网下载,导入JPushSDK。

安装导入过程请参照JPush官网开发文档。

App内部操作:

注册JPush

在AppDelegate didFinishLaunchingWithOptions 这个方法里面注册JPush。

JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定义categories
        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    BOOL isProduction = YES;
#if DEBUG
    isProduction = NO;//开启开发模式。
#endif
    [JPUSHService setupWithOption:launchOptions appKey:appKey channel:nil apsForProduction:isProduction];

注册返回

#pragma mark - 注册返回
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [JPUSHService registerDeviceToken:deviceToken];
}

适配iOS10

需要添加如下代码:
需要NSFoundationVersionNumber_iOS_9_x_Max来进行判断
需要导入UserNotifications.framework

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

// 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];
        //这里执行点击通知的跳转方法
        DLog(@"iOS10 收到前台通知:")
    }
    completionHandler(UNNotificationPresentationOptionBadge); // 需要执行这个方法,选择是否提醒用户,有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];
        //这里执行点击通知的跳转方法
        DLog(@"iOS10 收到远程通知:");
    }
    completionHandler();  // 系统要求执行这个方法
}

#endif

需要在AppDelegate头部加上如下代码:

// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

消息推送点击

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [JPUSHService handleRemoteNotification:userInfo];
    //这下面执行的,是对接收到的aps,进行必要处理
    [self gt_application:application didReceiveRemoteNotification:userInfo];
}
// 收到远程推送通知时
- (void)gt_application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    NSString *content = [aps valueForKey:@"alert"];
    if (application.applicationState == UIApplicationStateInactive){
        //启动APP或者换醒APP,以及相关跳转
    }
}

以上基本就已经完成了对JPush的配置。
以下是必须要注意的问题

注意

1.如果你的应用开启了BackGround Modes 如图:

第一个API是后台获取(Background Fetch),该API允许开发者在一个周期间隔后进行特定的动作,如获取网络内容、更新程序界面等等。第二个API是远程通知 (Remote Notification),它是一个新特性,它在当新事件发生时利用推送通知(Push Notifications)去告知程序。这两个新特性都是在后台进行的,这样更加有利于多任务执行。

这里写图片描述

那么你在AppDelegate里面实现的如下方法
将不再执行

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

s

而是要实现:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    [JPUSHService handleRemoteNotification:userInfo];
    //对aps的处理,并且要执行回调,刷新后台状态。
    completionHandler(UIBackgroundFetchResultNewData);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值