我是靠谱客的博主 爱笑书包,最近开发中收集的这篇文章主要介绍极光推送集成问题记录,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

安装

npm install jpush-react-native --save

也需要安装jcore-react-native

npm install jcore-react-native --save

核心步骤参考:
https://github.com/jpush/jpush-react-native

使用的版本为


"jcore-react-native": "^1.8.0",
"jpush-react-native": "^2.8.1",

使用react-native 0.60+以后,基本不需要手动link了,android和ios端的依赖资源会自动link,ios端额外需要

cd ios && pod install

在RN端的APP.js中加入

 JPush.init();
//连接状态
this.connectListener = result => {
console.log('connectListener:' + JSON.stringify(result));
};
JPush.addConnectEventListener(this.connectListener);
//通知回调
this.notificationListener = result => {
console.log('notificationListener:' + JSON.stringify(result));
};
JPush.addNotificationListener(this.notificationListener);
//本地通知回调
this.localNotificationListener = result => {
console.log('localNotificationListener:' + JSON.stringify(result));
};
JPush.addLocalNotificationListener(this.localNotificationListener);
//自定义消息回调
this.customMessageListener = result => {
console.log('customMessageListener:' + JSON.stringify(result));
};
JPush.addCustomMessagegListener(this.customMessageListener);
//tag alias事件回调
this.tagAliasListener = result => {
console.log('tagAliasListener:' + JSON.stringify(result));
};
JPush.addTagAliasListener(this.tagAliasListener);
//手机号码事件回调
this.mobileNumberListener = result => {
console.log('mobileNumberListener:' + JSON.stringify(result));
};
JPush.addMobileNumberListener(this.mobileNumberListener);

Android

  • build.gradle
android {
defaultConfig {
applicationId "yourApplicationId"
//在此替换你的应用包名
...
manifestPlaceholders = [
JPUSH_APPKEY: "yourAppKey",
//在此替换你的APPKey
JPUSH_CHANNEL: "yourChannel"
//在此替换你的channel
]
}
}
dependencies {
...
implementation project(':jpush-react-native')
// 添加 jpush 依赖
implementation project(':jcore-react-native')
// 添加 jcore 依赖
}
  • AndroidManifest.xml
<meta-data
android:name="JPUSH_CHANNEL"
android:value="${JPUSH_CHANNEL}" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="${JPUSH_APPKEY}" />
  • MainApplication.java
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
JPushModule.registerActivityLifecycle(this);
}

这个时候如果出现

Could not invoke JPushModule.init

可能是Jcore没装好,需要额外配置下jcore

  • settings.gradle
include ':jcore-react-native'
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')
implementation project(path: ':jcore-react-native')

IOS端

  • profile增加Push Notifacations功能
  • 申请APNS证书,并下载本地添加到钥匙扣
  • Capability中添加Background Modes-Remote notifacation
  • AppDelegate.m中
#import <RCTJPushModule.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

注意这里可以区分开发环境和生产环境

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#if DEBUG
InitializeFlipper(application);
#endif
// JPush初始化配置
#ifdef DEBUG
NSLog(@" setupWithOption 0");
[JPUSHService setupWithOption:launchOptions appKey:@"xxxxxx"
channel:@"dev" apsForProduction:NO];
#else
NSLog(@" setupWithOption 1");
[JPUSHService setupWithOption:launchOptions appKey:@"xxxxxx"
channel:@"dev" apsForProduction:YES];
#endif
// APNS
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
if (@available(iOS 12.0, *)) {
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
// 自定义消息
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
// 地理围栏
[JPUSHService registerLbsGeofenceDelegate:self withLaunchOptions:launchOptions];
...}
//************************************************JPush start************************************************
//注册 APNS 成功并上报 DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
//iOS 7 APNS
- (void)application:(UIApplication *)application didReceiveRemoteNotification:
(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// iOS 10 以下 Required
NSLog(@"iOS 7 APNS");
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//iOS 10 前台收到消息
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// Apns
NSLog(@"iOS 10 APNS 前台收到消息");
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
}
else {
// 本地通知 todo
NSLog(@"iOS 10 本地通知 前台收到消息");
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
}
//需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
completionHandler(UNNotificationPresentationOptionAlert);
}
//iOS 10 消息事件回调
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// Apns
NSLog(@"iOS 10 APNS 消息事件回调");
[JPUSHService handleRemoteNotification:userInfo];
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
[[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
}
else {
// 本地通知
NSLog(@"iOS 10 本地通知 消息事件回调");
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
[[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
}
// 系统要求执行这个方法
completionHandler();
}
//自定义消息
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
}
//************************************************JPush end************************************************
  • 将证书配置到极光平台

在钥匙扣中选择对应的证书,右键导出.p12文件,在极光应用配置中上传证书和密码

最后

以上就是爱笑书包为你收集整理的极光推送集成问题记录的全部内容,希望文章能够帮你解决极光推送集成问题记录所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部