• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

react-native-umeng-push是一个react native的友盟推送

react-native-umeng-push

安装

rnpm install react-native-umeng-push

集成到iOS

Appdelegate.m中对应的位置添加如下三个API:

#import "RCTUmengPush.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  //注册友盟推送
  [RCTUmengPush registerWithAppkey:@"your app key" launchOptions:launchOptions];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  //获取deviceToken
  [RCTUmengPush application:application didRegisterDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  //获取远程推送消息
  [RCTUmengPush application:application didReceiveRemoteNotification:userInfo];
}

参考: https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW6

启用推送设置 Enabling Push Notifications(否则会报 iOS device_token 无效)

集成到android

注意:0.29版本以后,reactNative会自动创建MainApplication,并且将添加原生模块从MainActivity移到了MainApplication,详情请见http://reactnative.cn/post/1774,所以我们的这里继承也有些变化,如果你的reactNative版本是0.29以下,请点击README-pre.md

1、添加PushSDK

由于这个库依赖于react-native-umeng-sdk,需要在你的工程settings.gradle文件中添加PushSDK

include ':PushSDK'
project(':PushSDK').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-umeng-sdk/android/PushSDK')

2、设置Application

新版本的react-native工程已经存在MainApplication,我们只需要将MainApplication继承UmengApplication,然后添加对应的UmengPushPackage进去就行了,如下所示:

import com.liuchungui.react_native_umeng_push.UmengPushApplication;
import com.liuchungui.react_native_umeng_push.UmengPushPackage;

public class MainApplication extends UmengPushApplication implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new UmengPushPackage()
      );
    }
  };

注:这一步主要是因为友盟推送需要在Application当中接收推送,UmengPushApplication封装了友盟推送的内容。如果友盟推送如果不放在Application当中,退出应用之后是无法接收到推送的。

3、添加AppKey & Umeng Message Secret

在项目工程的AndroidManifest.xml中的Application标签下添加:

<meta-data
    android:name="UMENG_APPKEY"
    android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxx" >
</meta-data>
<meta-data
    android:name="UMENG_MESSAGE_SECRET"
    android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxx" >
</meta-data>

4、安卓集成获取不到deviceToken问题

  • 确定是否将appkey、MessageSecret、以及包名都更换为开发者所申请的相应值
  • 如果获取不到deviceToken也接收不到推送,请查看友盟后台的包名是否一致,当前设备是否添加到测试设备当中
  • Android Studio中gradle的版本需要在1.5.0或者以上

更多DeviceToken相关问题,请参考Device_token 相关问题整理【安卓版】

5、其它

**注:**如果是android6.0以上的api编译,需要在PushSDK的build.gradle文件的android{}块内添加useLibrary 'org.apache.http.legacy',并把compileSdkVersion的版本号改为23。

详情参考:友盟安卓SDK集成指南

API

API Note
getDeviceToken 获取DeviceToken
didReceiveMessage 接收到推送消息回调的方法
didOpenMessage 点击推送消息打开应用回调的方法

Usage

import UmengPush from 'react-native-umeng-push';

//获取DeviceToken
UmengPush.getDeviceToken(deviceToken => {
    console.log("deviceToken: ", deviceToken);
});

//接收到推送消息回调
UmengPush.didReceiveMessage(message => {
    console.log("didReceiveMessage:", message);
});

//点击推送消息打开应用回调
UmengPush.didOpenMessage(message => {
    console.log("didOpenMessage:", message);
});

具体使用详情,请下载代码查看Example

Example

git clone https://github.com/liuchungui/react-native-umeng-push.git
cd react-native-umeng-push/Example
npm install --save

More

  • 欢迎大家Pull Request
  • 有什么疑问,欢迎提问题
  • 觉得好的,来一个star

More Repositories

1

react-native-BGNativeModuleExample

A native module example
Objective-C
53
star
2

BGNetwork

BGNetwork is a request util based on AFNetworking.
Objective-C
43
star
3

rn_bycloud

A react-native opensource project
JavaScript
37
star
4

HTTPSConnectDemo

使用OC和Swift两种语言,通过NSURLSession进行HTTPS验证
Objective-C
28
star
5

UICollectionViewAnimationDemo

一个demo
Swift
27
star
6

react-native-star-rating

A React Native component for generating and displaying interactive star ratings. Compatible with both iOS and Android.
JavaScript
19
star
7

UICollectionViewDemo

A collectionView demo
Objective-C
15
star
8

BGWaterFlowView

一个基于UICollectionView封装瀑布流控件,内部有封装好刷新的视图
Objective-C
14
star
9

BGSession

BGSession is a lightweight local data storage base on NSUserDefaults
Objective-C
9
star
10

BGUnlockController

一个指纹+数字密码解锁控制器
Objective-C
8
star
11

BGCustomLayoutCollectionViewDemo

通过自定义布局实现grideView的demo
Objective-C
8
star
12

Word2VecDemo

实践了下Word2Vec
Python
5
star
13

react-native-android-intent

Intent fro android
Java
4
star
14

BGFoundationKit

一个swift基础框架
Swift
3
star
15

react-native-umeng-sdk

3
star
16

liuchungui.github.io

HTML
2
star
17

first

A personal cocoapods repo
Ruby
2
star
18

BGPhotoPickerController

一个基于Photos框架写的照片选择控制器
Swift
2
star
19

BGIpaBuilder

一个基于xcodebuild命令的ipa打包脚本
Shell
1
star
20

TestCocoaPodsProject

Objective-C
1
star
21

VFLDemo

This is a Demo
Objective-C
1
star
22

angularExample

a angular Example
HTML
1
star
23

TestLCGCocoapods

test it
Ruby
1
star
24

BGLayoutCollection

一个UICollectionView的layout集合
1
star
25

ik-analyzer

Automatically exported from code.google.com/p/ik-analyzer
Java
1
star
26

BGAnimations

A animation collection
Objective-C
1
star
27

AnimationForAutoLayoutDemo

This is a Demo
Objective-C
1
star