- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // 取得 APNs 标准信息内容 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; }
这个方法是清除icon角标
- (void)applicationWillEnterForeground:(UIApplication *)application { [application setApplicationIconBadgeNumber:0]; // [application cancelAllLocalNotifications]; }
//iOS 7 Remote Notification - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler { [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; }
// 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]; [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object: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]; [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; } completionHandler(); // 系统要求执行这个方法 }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }
如果想要获取到自定义消息的话,需要在didFinishLaunchingWithOptions方法中添加一下代码:
//获取自定义消息 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(networkDidReceiveMess
还需要添加新的方法,以监听自定义消息的接受:
//#pragma mark 获取自定义消息内容 - (void)networkDidReceiveMessage:(NSNotification *)notification { NSDictionary * userInfo = [notification userInfo]; NSString *content = [userInfo valueForKey:@"content"]; NSDictionary *extras = [userInfo valueForKey:@"extras"]; NSString *customizeField1 = [extras valueForKey:@"123456"]; //自定义参数,key是自己定义的 NSLog(@"自定义message:%@",userInfo); NSLog(@"推%@",content); NSLog(@"推%@",extras); NSLog(@"推%@",customizeField1); }
配置代码,在Xcode中打开push的权限
往下滑动,配置:
到此为止,ios的配置结束。
然后在RN中配置调用jpush的代码:
import JPushModule from 'jpush-react-native'; constructor(props) { super(props); if(Platform.OS === 'android') JPushModule.initPush(); } componentDidMount(){ if (Platform.OS === 'android') { BackAndroid.addEventListener('hardwareBackPress', this._onBackAndroid); //-----------jpush android start // JPushModule.getInfo((map) => { // console.log(map); // }); // JPushModule.addReceiveCustomMsgListener((message) => { // }); JPushModule.addReceiveNotificationListener((message) => { console.log("receive notification: "); console.log(message); }); JPushModule.addReceiveOpenNotificationListener((map) => { console.log("Opening notification!"); console.log(map); }); //-----------jpush android end } //-----------jpush ios start if (Platform.OS === 'ios') { this.subscription = NativeAppEventEmitter.addListener( 'ReceiveNotification', (notification) => { console.log('-------------------收到推送----------------'); console.log(notification) } ); } //-----------jpush ios end } componentWillUnmount(){ if (Platform.OS === 'android') { BackAndroid.removeEventListener('hardwareBackPress', this._onBackAndroid); } JPushModule.removeReceiveCustomMsgListener(); JPushModule.removeReceiveNotificationListener(); this.subscription && this.subscription.remove(); }
然后就可以去官方控制台,手动推送通知了
想要icon右上角角标显示的数字增加,如图:
加号为英文状态下的
大家集成的时候多看官方文档,将两端的官方demo下载下来,能发现很多有用的信息。