npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@react-native-hero/umeng-push

v0.3.7

Published

react native umeng push

Downloads

67

Readme

@react-native-hero/umeng-push

Getting started

Install the library using either Yarn:

yarn add @react-native-hero/umeng-push

or npm:

npm install --save @react-native-hero/umeng-push

Link

  • React Native v0.60+

For iOS, use cocoapods to link the package.

run the following command:

$ cd ios && pod install

For android, the package will be linked automatically on build.

  • React Native <= 0.59

run the following command to link the package:

$ react-native link @react-native-hero/umeng-push

Setup

image

打开应用信息页面,安卓推送有 AppkeyUmeng Message Secret 两个字段,iOS 只有 Appkey 字段,后面将用这些字段初始化友盟。

iOS

确保推送证书配置正确。举个例子,如果你的 App 有两个版本:测试版和正式版,Bundle ID 分别是 com.abc.testcom.abc.prod,那么证书必须和 Bundle ID 对应。

打开 Xcode,开启推送。

image

修改 AppDelegate.m,如下

#import <RNTUmengPush.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...
  // 初始化友盟基础库
  // channel 一般填 App Store,如果有测试环境,可按需填写
  // debug 表示是否打印调试信息
  [RNTUmengPush init:@"appKey" channel:@"App Store" debug:false launchOptions:launchOptions];

  return YES;
}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [RNTUmengPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  [RNTUmengPush didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

Android

修改 android/build.gradle,如下:

allprojects {
    repositories {
        // 确保添加了华为和友盟仓库
        maven { url 'https://developer.huawei.com/repo/'}
        maven { url 'https://repo1.maven.org/maven2/' }
    }
}

android/app/build.gradle 根据不同的包填写不同的配置,如下:

android {
    buildTypes {
        debug {
            manifestPlaceholders = [
                UMENG_APP_KEY: '',
                UMENG_PUSH_SECRET: '',
                UMENG_CHANNEL: '',
                HUAWEI_PUSH_APP_ID: '',
                HONOR_PUSH_APP_ID: '',
                XIAOMI_PUSH_APP_ID: '',
                XIAOMI_PUSH_APP_KEY: '',
                OPPO_PUSH_APP_KEY: '',
                OPPO_PUSH_APP_SECRET: '',
                VIVO_PUSH_APP_ID: '',
                VIVO_PUSH_APP_KEY: '',
                MEIZU_PUSH_APP_ID: '',
                MEIZU_PUSH_APP_KEY: '',
            ]
        }
        release {
            manifestPlaceholders = [
                UMENG_APP_KEY: '',
                UMENG_PUSH_SECRET: '',
                UMENG_CHANNEL: '',
                HUAWEI_PUSH_APP_ID: '',
                HONOR_PUSH_APP_ID: '',
                XIAOMI_PUSH_APP_ID: '',
                XIAOMI_PUSH_APP_KEY: '',
                OPPO_PUSH_APP_KEY: '',
                OPPO_PUSH_APP_SECRET: '',
                VIVO_PUSH_APP_ID: '',
                VIVO_PUSH_APP_KEY: '',
                MEIZU_PUSH_APP_ID: '',
                MEIZU_PUSH_APP_KEY: '',
            ]
        }
    }
}

配置厂商通道请先阅读官方文档,主要是获取各个通道的 appIdappKeyappSecret 等数据,并保存到友盟后台的应用信息里。

MainApplicationonCreate 方法进行初始化,如下:

override fun onCreate() {

    // 配置厂商通道 Activity 展示的页面
    UmengPushActivity.activityView = R.layout.splash_screen_default
    // 配置厂商通道 Activity 跳转的 mainActivity
    UmengPushActivity.mainActivityClass = MainActivity::class.java

    val metaData = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).metaData

    // 初始化友盟基础库
    // 第三个参数表示是否显示调试信息
    RNTUmengPushModule.init(this, metaData, false)
}

配置混淆规则

android/app/proguard-rules.pro 添加以下混淆规则,注意替换自己的包名,并且删掉 []

-keep public class [您的应用包名].R$*{
public static final int *;
}

配置华为、小米、魅族厂商通道使用的 Activity

打开指定页面 填入 ${包名}.UmengPushActivity,比如 com.abc.UmengPushActivity

注意,如果在打包阶段用了别的包名,需改为对应的包名。

解决魅族的兼容问题

drawable 目录下添加一个图标,命名为 stat_sys_third_app_notify.png,建议尺寸 64px * 64px,图标四周留有透明。若不添加此图标,可能在部分魅族手机上无法弹出通知。

Usage

import {
  ALIAS_TYPE,
  NOTIFICATION_PLAY,
  init,
  start,
  getTags,
  addTags,
  removeTags,
  setAlias,
  addAlias,
  removeAlias,
  setAdvanced,
  addListener,
  supportDisable,
  enable,
  disable,
  supportNotificationChannel,
  createNotificationChannel,
  deleteNotificationChannel,
} from '@react-native-hero/push'

// 注册获取 device token
addListener(
  'register',
  function (data) {
    data.deviceToken
    // 如果 app 未启动状态下,点击推送打开 app,会有两个新字段
    // 点击的推送
    data.notification
    // 推送的自定义参数
    // 字符 d、p 为友盟保留字段,不能作为自定义参数的 key,value 只能是字符串类型,
    // 字符总和不能超过 1000 个字符
    data.custom
  }
)

// 远程推送
addListener(
  'remoteNotification',
  function (data) {
    // 如果点击了推送,data.clicked 是 true
    data.clicked
    // 如果推送送达并展示了,data.presented 是 true
    data.presented

    // 推送详情,如标题、内容
    data.notification
    // 推送的自定义参数
    // 字符 d、p 为友盟保留字段,不能作为自定义参数的 key,value 只能是字符串类型,
    // 字符总和不能超过 1000 个字符
    data.custom
  }
)

// 透传消息
// ios 通过静默推送实现,例子:payload: { aps: { 'content-available': 1 }, key1: 'value1' }
// android 通过自定义消息实现,例子:payload: { display_type: 'message', body: { custom: '' }, extra: { key1: 'value1' } }
//
// 注意:ios aps 下面不能包含 alert、badge、sound 等字段,如果包含了其中任何一个,就会变成通知,即会显示在通知栏。
// 你也可以加上 alert,比如 aps: { alert: '你有一条新消息', 'content-available': 1 },这样还是会走进 message 事件回调里,只是展现为通知形式
addListener(
  'message',
  function (data) {

    // ios 用于静默推送实现消息透传
    // 静默推送不会在通知栏展现,这一点很符合透传消息的要求
    // 但是苹果要求静默推送的 aps 对象下不能包含 alert、badge、sound 等字段
    // 如果我们加了 alert,它就会变成通知,会展现在通知栏里
    // 有时候,你会希望加上 alert,类似 aps: { alert: '你有一条新消息', 'content-available': 1 }
    // 这时依然会触发 message 事件回调,只是会带上 remoteNotification 特有的 clicked 或 presented 属性
    // 如果你希望和安卓表现一样,只需要过滤掉 clicked 即可,如下
    if (data.clicked) {
      return
    }

    // 这样只有当 app 在前台时,才会响应透传消息

    // 自定义参数
    // 字符 d、p 为友盟保留字段,不能作为自定义参数的 key,value 只能是字符串类型,
    // 字符总和不能超过 1000 个字符
    // 即例子中的 { key1: 'value1' }
    data.custom

    // alert 或 custom 字段中的字符串值
    data.message

  }
)

// 对于安卓来说,需要等用户同意隐私政策后,再调用 init,js 的 init 才是真正的初始化
// https://developer.umeng.com/docs/67966/detail/207155
// 对于 ios 来说,如果无需使用『高级设置』,则可以不调用 init 方法
init({
  // 『安卓』app 在前台时是否显示推送
  notificationOnForeground: true,
  // 『安卓』填 AndroidManifest.xml 中 package 的值
  resourcePackageName: 'com.abc',
  // 『安卓』开启厂商推送通道,按需开启即可
  huaweiEnabled: true,
  honorEnabled: true,
  xiaomiEnabled: true,
  oppoEnabled: true,
  vivoEnabled: true,
  meizuEnabled: true,
  // 文档最后 『高级设置』中的所有配置项都可以适用于 init,方便在初始化时一次性做好配置
})

// 注意!!!
// 下面的所有方法需在 init 调用之后才可调用

// 启动
// 调用 start 之后才会触发 register 事件
// 因此注册 register 事件应早于 start
start()

// 下面这些方法的具体用法和注意事项,请参考文档
// https://developer.umeng.com/docs/67966/detail/98583#h1--tag-alias-4
getTags().then(data => {
  // success
  data.tags
})
.catch(err => {
  // failure
})

addTags(['tag1', 'tag2']).then(data => {
  // success
  data.remain
})
.catch(err => {
  // failure
})

removeTags(['tag1', 'tag2']).then(data => {
  // success
  data.remain
})
.catch(err => {
  // failure
})


// type 如果是第三方帐号,使用导入的常量 ALIAS_TYPE.XXX
// 如果是自建帐号体系,可传入自己产品的英文名或拼音
setAlias('alias', 'type').then(data => {
  // success
})
.catch(err => {
  // failure
})

addAlias('alias', 'type').then(data => {
  // success
})
.catch(err => {
  // failure
})

removeAlias('alias', 'type').then(data => {
  // success
})
.catch(err => {
  // failure
})

// 高级设置
setAdvanced({
  // ios: 当应用在前台时收到推送是否弹出 Alert,默认弹出
  autoAlert: true,
  // ios: 是否允许 SDK 自动清空角标,默认自动角标清零
  badgeClear: true,

  // android: 最多显示通知的条数,当显示数目大于设置值时,若再有新通知到达,会移除一条最早的通知,只为 0-10,0 表示不限制个数
  displayNotificationNumber: 0,
  // android: 默认情况下,同一台设备在1分钟内收到同一个应用的多条通知时,不会重复提醒,同时在通知栏里新的通知会替换掉旧的通知,可以通过此配置来设置冷却时间
  muteDurationSeconds: 60,
  // android: 是否响铃,使用 NOTIFICATION_PLAY 枚举值
  notificationPlaySound: NOTIFICATION_PLAY.SERVER,
  // android: 是否点亮呼吸灯,使用 NOTIFICATION_PLAY 枚举值
  notificationPlayLights: NOTIFICATION_PLAY.SERVER,
  // android: 是否振动,使用 NOTIFICATION_PLAY 枚举值
  notificationPlayVibrate: NOTIFICATION_PLAY.SERVER,
  // android: 设置免打扰时间段,期间收到通知消息时不响铃,不闪灯,不振动
  noDisturbStartHour: 23,
  noDisturbStartMinute: 0,
  noDisturbEndHour: 7,
  noDisturbEndMinute: 0,
})


// 是否支持关闭推送
// 初始化 SDK 后,默认是开启推送状态,如果需要开关,可借鉴以下代码
if (supportDisable) {
  // 关闭推送
  disable()
  // 开启推送
  enable()
}

// 是否支持推送通道(安卓 >= 8.0支持推送通道)
if (supportNotificationChannel) {
  // 创建推送通道
  // 如果已创建该通道(通过 id 判断),则无视此次调用
  createNotificationChannel({
    id: 'id',
    name: 'name',
    importance: 'max|high|low|min|default',
    // 下面都是可选字段
    // 具体参考 https://blog.csdn.net/bingeho/article/details/124454006
    description: 'description',
    lockscreenVisibility: 'secret|private|public',
    enableLights: true|false,
    enableVibration: true|false,
    setShowBadge: true|false,
    groupId: 'groupId',
    groupName: 'groupName',
  })
  // 删除推送通道
  deleteNotificationChannel({
    id: 'id'
  })
}