<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        React Native 集成jpush-react-native的示例代碼

        來源:懂視網 責編:小采 時間:2020-11-27 22:32:36
        文檔

        React Native 集成jpush-react-native的示例代碼

        React Native 集成jpush-react-native的示例代碼:jpush-React-native是極光推送官方維護的一個插件,github地址:https://github.com/jpush/jpush-react-native 一.手動配置 1.集成插件到項目中 npm install jpush-react-native --save rnpm link jpush-rea
        推薦度:
        導讀React Native 集成jpush-react-native的示例代碼:jpush-React-native是極光推送官方維護的一個插件,github地址:https://github.com/jpush/jpush-react-native 一.手動配置 1.集成插件到項目中 npm install jpush-react-native --save rnpm link jpush-rea

        jpush-React-native是極光推送官方維護的一個插件,github地址:https://github.com/jpush/jpush-react-native

        一.手動配置

        1.集成插件到項目中

        npm install jpush-react-native --save
        rnpm link jpush-react-native

        Android

        使用 android Studio import 你的 react Native 應用(選擇你的 React Native 應用所在目錄下的 android 文件夾即可)

        修改 android 項目下的 settings.gradle 配置: settings.gradle

        include ':app', ':jpush-react-native'
        project(':jpush-react-native').projectDir = new File(rootProject.projectDir,'../node_modules/jpush-react-native/android') 

        修改 app 下的 AndroidManifest 配置,將 jpush 相關的配置復制到這個文件中,參考 demo 的 AndroidManifest:(增加了 部分)

        your react native project/android/app/AndroidManifest.xml

        <application
         android:name=".MainApplication"
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme">
         <activity
         android:name=".MainActivity"
         android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
         android:label="@string/app_name">
         <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         </activity>
        
         <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
        
         <!-- Required . Enable it you can get statistics data with channel -->
         <meta-data android:name="JPUSH_CHANNEL" android:value="${APP_CHANNEL}"/>
         <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}"/>
        
         </application>
        
        

        修改 app 下的 build.gradle 配置: your react native project/android/app/build.gradle

        修改 app 下的 build.gradle 配置:

        your react native project/android/app/build.gradle
        android {
         defaultConfig {
         applicationId "yourApplicationId"
         ...
         manifestPlaceholders = [
         JPUSH_APPKEY: "yourAppKey", //在此替換你的APPKey
         APP_CHANNEL: "developer-default" //應用渠道號
         ]
         }
        }
        ...
        dependencies {
         compile fileTree(dir: "libs", include: ["*.jar"])
         compile project(':jpush-react-native')
         compile "com.facebook.react:react-native:+" // From node_modules
        }
        

        將此處的 yourApplicationId 替換為你的項目的包名;yourAppKey 替換成你在官網上申請的應用的 AppKey。

        現在重新 sync 一下項目,應該能看到 jpush-react-native 作為一個 android Library 項目導進來了

        重點來了,我在這個地方卡了一天,以上代碼配置完成后,但是不管怎么樣就是接收不到推送。

        解決方案:找到項目/node_modules/jpush-react-native/android/src/main/AndroidManifest.xml,里面的 ${applicationId} 全部換成 你自己的項目包名

        到此為止android的配置結束。

        二.iOS配置

        打開 ios 工程,在 rnpm link 之后,RCTJPushModule.xcodeproj 工程會自動添加到 Libraries 目錄里面

        在 iOS 工程 target 的 Build Phases->Link Binary with Libraries 中加入如下庫:

        CFNetwork.framework
        CoreFoundation.framework
        CoreTelephony.framework
        SystemConfiguration.framework
        CoreGraphics.framework
        Foundation.framework
        UIKit.framework
        Security.framework
        libz.tbd (Xcode7以下版本是libz.dylib)
        UserNotifications.framework (Xcode8及以上)
        libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)

        根據域名配置info.plist:

        把需要的支持的域添加給NSExceptionDomains。其中jpush.cn作為Key,類型為字典類型。

        每個域下面需要設置2個屬性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。

        兩個屬性均為Boolean類型,值分別為YES、YES。

        如圖

        在 AppDelegate.h 文件中 填寫如下代碼,這里的的 appkey、channel、和 isProduction 填寫自己的

        static NSString *appKey = @”“; //填寫appkey

        static NSString *channel = @”“; //填寫channel 一般為nil

        static BOOL isProduction = false; //填寫isProdurion 平時測試時為false ,生產時填寫true

        在AppDelegate.m 里面添加如下代碼

        1.引入依賴文件

         #import <RCTJPushModule.h>
         #ifdef NSFoundationVersionNumber_iOS_9_x_Max
         #import <UserNotifications/UserNotifications.h>
         #endif
        
         @interface AppDelegate()
        
         @end
        

        2.在didFinishLaunchingWithOptions方法里添加

        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        
         JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        
         entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
        
         [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        
         }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        
         //可以添加自定義categories
         [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge |
         UNAuthorizationOptionSound |
         UNAuthorizationOptionAlert)
         categories:nil];
         }else {
        
         //categories 必須為nil
         [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge |
         UNAuthorizationOptionSound |
         UNAuthorizationOptionAlert)
         categories:nil];
         }
        
         [JPUSHService setupWithOption:launchOptions appKey:appKey
         channel:nil apsForProduction:isProduction];
        
        

        3.加入jupush的代碼

        - (void)application:(UIApplication *)application
        
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        
         [JPUSHService registerDeviceToken:deviceToken];
        
        }
        
        - (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下載下來,能發現很多有用的信息。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        React Native 集成jpush-react-native的示例代碼

        React Native 集成jpush-react-native的示例代碼:jpush-React-native是極光推送官方維護的一個插件,github地址:https://github.com/jpush/jpush-react-native 一.手動配置 1.集成插件到項目中 npm install jpush-react-native --save rnpm link jpush-rea
        推薦度:
        標簽: rea 集成 React
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: xx视频在线永久免费观看| 国产综合免费精品久久久| mm1313亚洲精品无码又大又粗| 91亚洲性爱在线视频| 69av免费视频| 亚洲人成www在线播放| 成年女人视频网站免费m| 国产亚洲精品bv在线观看| 最近的免费中文字幕视频| 亚洲精品国产综合久久久久紧| 狠狠入ady亚洲精品| 国产免费牲交视频| 免费无码午夜福利片69| 西西大胆无码视频免费| 亚洲人成色在线观看| 日本免费在线观看| 亚洲综合综合在线| 成在人线AV无码免费| 老外毛片免费视频播放| 亚洲成在人线av| 国产在线观看麻豆91精品免费 | 国产免费观看青青草原网站| 美女一级毛片免费观看| 亚洲精品美女久久777777| 无人在线观看免费高清| 亚洲а∨天堂久久精品9966| 免费a级毛片无码a∨性按摩| 男人天堂免费视频| 亚洲人成网网址在线看| 亚洲国产成人影院播放| 一级毛片在线免费看| 亚洲熟妇AV一区二区三区浪潮| 亚洲精品乱码久久久久久蜜桃| 在线成人爽a毛片免费软件| 亚洲AV无码成人精品区日韩 | 亚洲色中文字幕在线播放| 亚洲AV无码乱码精品国产| 182tv免费视频在线观看| 最新国产成人亚洲精品影院| 99久久久国产精品免费牛牛 | 2021在线永久免费视频|