/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
你的运行环境
IPad Mini2 iOS11.0.3
期望效果
iOS设备和模拟器,执行下面的代码,可以正确获取registrationId
实际效果
JPushModule.getRegistrationID的callback从不执行。而iOS模拟器可以正确获取。
重现步骤
import "AppDelegate.h"
import
ifdef NSFoundationVersionNumber_iOS_9_x_Max
import <UserNotifications/UserNotifications.h>
endif
import <React/RCTBundleURLProvider.h>
import <React/RCTRootView.h>
@implementation AppDelegate
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { JPUSHRegisterEntity entity = [[JPUSHRegisterEntity alloc] init]; entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; [JPUSHService setupWithOption:launchOptions appKey:@"已屏蔽" channel:nil apsForProduction:nil]; NSURL jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"FWApp_RN" initialProperties:nil launchOptions:launchOptions]; rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; return YES; }
(void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken { [JPUSHService registerDeviceToken:deviceToken]; }
(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo { [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; }
(void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification { [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object: notification.userInfo]; }
(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler { [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; }
(void)jpushNotificationCenter:(UNUserNotificationCenter )center willPresentNotification:(UNNotification )notification withCompletionHandler:(void (^)(NSInteger))completionHandler { NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; } completionHandler(UNNotificationPresentationOptionAlert); }
(void)jpushNotificationCenter:(UNUserNotificationCenter )center didReceiveNotificationResponse:(UNNotificationResponse )response withCompletionHandler:(void (^)())completionHandler { NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; [[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotification object:userInfo]; } completionHandler(); } @end
export default class App extends Component { render() { return (
); }
componentDidMount () { this._initJPush(); }
_initJPush() { if (Platform.OS === 'android') { JPushModule.initPush(); JPushModule.getInfo(map => { this.setState({ appkey: map.myAppKey, imei: map.myImei, package: map.myPackageName, deviceId: map.myDeviceId, version: map.myVersion }) }); JPushModule.notifyJSDidLoad(resultCode => { if (resultCode === 0) { } }) } else { JPushModule.setupPush(); }
JPushModule.addReceiveCustomMsgListener(map => { this.setState({ pushMsg: map.message }); console.log('extras: ' + map.extras) });
JPushModule.addReceiveNotificationListener(map => { console.log('alertContent: ' + map.alertContent) console.log('extras: ' + map.extras) // var extra = JSON.parse(map.extras); // console.log(extra.key + ": " + extra.value); })
JPushModule.addReceiveOpenNotificationListener(map => { console.log('Opening notification!') console.log('map.extra: ' + map.extras) this.jumpSecondActivity() // JPushModule.jumpToPushActivity("SecondActivity"); })
JPushModule.addGetRegistrationIdListener(registrationId => { console.log('Device register succeed, registrationId ' + registrationId) })
// var notification = { // buildId: 1, // id: 5, // title: 'jpush', // content: 'This is a test!!!!', // extra: { // key1: 'value1', // key2: 'value2' // }, // fireTime: 2000 // } // JPushModule.sendLocalNotification(notification) }
componentWillUnmount () { JPushModule.removeReceiveCustomMsgListener(receiveCustomMsgEvent) JPushModule.removeReceiveNotificationListener(receiveNotificationEvent) JPushModule.removeReceiveOpenNotificationListener(openNotificationEvent) JPushModule.removeGetRegistrationIdListener(getRegistrationIdEvent) console.log('Will clear all notifications') JPushModule.clearAllNotifications() }
componentDidMount() { this.initJPush(); } initJPush() { JPushModule.getRegistrationID(registrationId => { console.log(registrationId); this.setState({ registrationId: registrationId }) }); }
Debug logs
包括 Android 或 iOS 的日志: