最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react-native使用leanclound消息推送的方法

 更新時間:2018年08月06日 15:11:11   作者:ding_ios  
這篇文章主要介紹了react-native使用leanclound消息推送的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

iOS消息推送的基本流程

1.注冊:為應(yīng)用程序申請消息推送服務(wù)。此時你的設(shè)備會向APNs服務(wù)器發(fā)送注冊請求。2. APNs服務(wù)器接受請求,并將deviceToken返給你設(shè)備上的應(yīng)用程序 3.客戶端應(yīng)用程序?qū)eviceToken發(fā)送給后臺服務(wù)器程序,后臺接收并儲存。 4.后臺服務(wù)器向APNs服務(wù)器發(fā)送推送消息 5.APNs服務(wù)器將消息發(fā)給deviceToken對應(yīng)設(shè)備上的應(yīng)用程序

使用leanclound進行消息推送

優(yōu)勢:文檔清晰,價格便宜

接入Leanclound

1.首先需要創(chuàng)建一個react-native項目

react-native init projectName

2.在leancloud創(chuàng)建一個同名項目,并記錄好appid和appkey

3.項目創(chuàng)建成功后,安裝推送所需的模塊(需要cd到工程目錄)

1.使用yarn安裝

yarn add leancloud-storage
yarn add leancloud-installation

2.使用npm安裝

npm install leancloud-storage --save
npm install leancloud-installation --save

4.在項目目錄下新建一個文件夾,名字為pushservice,在里面添加一個js文件PushService.js,處理消息推送的邏輯,

1.在index.js初始化leanclound

import AV from 'leancloud-storage'
...
/*
*添加注冊的appid和appkey
*/
const appId = 'HT23EhDdzAfFlK9iMTDl10tE-gzGzoHsz'
const appKey = 'TyiCPb5KkEmj7XDYzwpGIFtA'
/*
*初始化
*/
AV.initialize(appId,appKey);
/*
*把Installation設(shè)為全局變量,在其他文件方便使用
*/
global.Installation = require('leancloud-installation')(AV);

...

2.iOS端配置

首先,在項目中引入RCTPushNotification,詳情請參考: Linking Libraries - React Native docs

步驟一:將PushNotification項目拖到iOS主目錄,PushNotification路徑:當(dāng)前項目/node_modules/react-native/Libraries/PushNotificationIOS目錄下

步驟二:添加libRCTPushNotification靜態(tài)庫,添加方法:工程Targets-Build Phases-link binary with Libraries 點擊添加,搜索libRCTPushNotification.a并添加

步驟三:開啟推送功能,方法:工程Targets-Capabilities 找到Push Notification并打開

步驟四:在Appdelegate.m文件添加代碼

#import <React/RCTPushNotificationManager.h>
...

//注冊推送通知
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
 [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
 
 [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
 NSLog(@"收到通知:%@",userInfo);
 [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
 NSLog(@"error == %@" , error);
 [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
 NSLog(@"接受通知:%@",notification);
 [RCTPushNotificationManager didReceiveLocalNotification:notification];
}

5. 獲取deviceToken,并將deviceToken插入到_Installation

找到PushService文件,編寫代碼

//引用自帶PushNotificationIOS
const PushNotificationIOS = require('react-native').PushNotificationIOS;
...
class PushService {
 //初始化推送
 init_pushService = () => {
  //添加監(jiān)聽事件
  PushNotificationIOS. addEventListener('register',this.register_push);
  //請求權(quán)限
  PushNotificationIOS.requestPermissions();
 }
 //獲取權(quán)限成功的回調(diào)
 register_push = (deviceToken) => {
  //判斷是否成功獲取到devicetoken
  if (deviceToken) {
   this.saveDeviceToken(deviceToken);
  } 
 }
 //保存devicetoken到Installation表中
 saveDeviceToken = (deviceToken) => {
  global.Installation.getCurrent()
   .then(installation => {
   installation.set('deviceType', 'ios');
   installation.set('apnsTopic', '工程bundleid');
   installation.set('deviceToken', deviceToken);
   return installation.save();
  });
 }
 
}

修改App.js文件 在componentDidMount初始化推送

import PushService from './pushservice/PushService';
...
componentDidMount () {
 //初始化
 PushService.init_pushService();
}

運行項目,必須真機才能獲取到deviceToken,模擬器獲取不到,看看是否保存的deviceToken,如果保存成功,leandclound后臺能發(fā)現(xiàn)_Installation表多了一條數(shù)據(jù)

進行到這步了就已經(jīng)完成了一半了,現(xiàn)在只需要配置推送證書即可接收推送消息,這里就不介紹配置證書流程了,詳細(xì)步驟請參考 iOS推送證書設(shè)置,推送證書設(shè)置完成后,現(xiàn)在就去leanclound試試看能不能收到推送吧,退出APP,讓APP處于后臺狀態(tài),

點擊發(fā)送,看是不是收到了消息.

進行到這步驟說明推送已經(jīng)完成了一大半了,APP當(dāng)然還需要包括以下功能:

  • APP在前臺、后臺或者關(guān)閉狀態(tài)下也能收到推送消息
  • 點擊通知能夠?qū)ο⑦M行操作,比如跳轉(zhuǎn)到具體頁面

APP處于前臺狀態(tài)時通知的顯示

當(dāng)APP在前臺運行時的通知iOS是不會提醒的(iOS10后開始支持前臺顯示),因此需要實現(xiàn)的功能就是收到通知并在前端顯示,這時候就要使用一個模塊來支持該功能了,那就是react-native-message-bar

首先就是安裝react-native-message-bar模塊了

yarn add react-native-message-bar //yarn安裝
或者
npm install react-native-message-bar --save //npm安裝

安裝成功之后,在App.js文件中引入并注冊MessageBar

...
/*
*引入展示通知模塊
 */
const MessageBarAlert = require('react-native-message-bar').MessageBar;
const MessageBarManager = require('react-native-message-bar').MessageBarManager;
...
componentDidMount () {
 //初始化
 PushService.init_pushService();
 MessageBarManager.registerMessageBar(this.alert);
}
...
render() {
 const {Nav} = this.state
 if (Nav) {
  return (
  //這里用到了導(dǎo)航,所以需要這樣寫,布局才不會亂 MessageBarAlert綁定一個alert
  <View style={{flex: 1,}}>
   <Nav />
   <MessageBarAlert ref={(c) => { this.alert = c }} />
  </View>
  )
 }
 return <View />
 }

然后再對PushService進行修改,新增對notification事件監(jiān)聽和推送消息的展示

import { AppState, NativeModules, Alert, DeviceEventEmitter } from 'react-native';
 ...
 //初始化推送
 init_pushService = () => {
  //添加監(jiān)聽事件
  PushNotificationIOS. addEventListener('register',this.register_push);
  PushNotificationIOS.addEventListener('notification', this._onNotification);
  //請求權(quán)限
  PushNotificationIOS.requestPermissions();
 }
 _onNotification = ( notification ) => {
  var state = AppState.currentState;
  // 判斷當(dāng)前狀態(tài)是否是在前臺
  if (state === 'active') {
   this._showAlert(notification._alert);
  }
 }
 ...
 _showAlert = ( message ) => {
  const MessageBarManager = require('react-native-message-bar').MessageBarManager;
  MessageBarManager.showAlert({
  title: '您有一條新的消息',
  message: message,
  alertType: 'success',
  stylesheetSuccess: {
   backgroundColor: '#7851B3', 
   titleColor: '#fff', 
   messageColor: '#fff'
  },
  viewTopInset : 20
 });

 }

 ...

最后重新運行APP并在leanclound發(fā)送一條消息,看是否在APP打開狀態(tài)也能收到通知,到了這里推送就完成了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • react-router-dom的使用說明

    react-router-dom的使用說明

    這篇文章主要介紹了react-router-dom的使用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 在React項目中使用iframe嵌入一個網(wǎng)站的步驟

    在React項目中使用iframe嵌入一個網(wǎng)站的步驟

    本文介紹了如何在React項目中通過iframe嵌入百度網(wǎng)站的步驟,首先創(chuàng)建一個Baidu.js組件,并在該組件中設(shè)置iframe來加載百度,然后在App.js中引入并使用Baidu組件,還討論了因安全策略可能無法加載某些網(wǎng)站的問題,需要的朋友可以參考下
    2024-09-09
  • React?代碼拆分的幾種方法示例詳解

    React?代碼拆分的幾種方法示例詳解

    這篇文章主要為大家介紹了React?代碼拆分的幾種方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 基于react組件之間的參數(shù)傳遞(詳解)

    基于react組件之間的參數(shù)傳遞(詳解)

    下面小編就為大家?guī)硪黄趓eact組件之間的參數(shù)傳遞(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • webpack構(gòu)建react多頁面應(yīng)用詳解

    webpack構(gòu)建react多頁面應(yīng)用詳解

    這篇文章主要介紹了webpack構(gòu)建react多頁面應(yīng)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • React翻頁器的實現(xiàn)(包含前后端)

    React翻頁器的實現(xiàn)(包含前后端)

    本文主要介紹了React翻頁器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 解決React報錯JSX?element?type?does?not?have?any?construct?or?call?signatures

    解決React報錯JSX?element?type?does?not?have?any?construct

    這篇文章主要為大家介紹了解決React報錯JSX?element?type?does?not?have?any?construct?or?call?signatures,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • React中key屬性的警告及解決方案

    React中key屬性的警告及解決方案

    在使用 React 進行開發(fā)時,key 屬性是一個至關(guān)重要的概念,尤其在渲染列表時,開發(fā)者在使用 key 屬性時,常常會遇到各種警告信息,本文將詳細(xì)解析這些警告的原因,提供有效的解決方案,并總結(jié)最佳實踐,需要的朋友可以參考下
    2024-12-12
  • React實現(xiàn)動態(tài)輪播圖的使用示例

    React實現(xiàn)動態(tài)輪播圖的使用示例

    輪播組件是常見的一種方式,用來展示圖像、信息或者是廣告,本文就來介紹一下React實現(xiàn)動態(tài)輪播圖的使用示例,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 詳解react-router4 異步加載路由兩種方法

    詳解react-router4 異步加載路由兩種方法

    本篇文章主要介紹了詳解react-router4 異步加載路由兩種方法 ,具有一定的參考價值,有興趣的可以了解一下
    2017-09-09

最新評論

揭东县| 中阳县| 临颍县| 三亚市| 乳山市| 沁阳市| 深圳市| 巴楚县| 福泉市| 利辛县| 藁城市| 平果县| 搜索| 瓦房店市| 顺平县| 册亨县| 安远县| 松桃| 三亚市| 福建省| 和硕县| 泸水县| 河东区| 九江县| 祁连县| 宁蒗| 平湖市| 德阳市| 安多县| 诸暨市| 阜阳市| 麻城市| 汉寿县| 县级市| 大丰市| 嘉善县| 滨海县| 三明市| 东辽县| 赤峰市| 中超|