消息通知
前言
消息推送对于大多数app来说这部分功能都是需要的,但是目前常用的推送基本在于:极光推送,个推等;一般的极光推送的集成网上资料基本很多,主要集成过程也比较完全,本篇文章介绍极光推送消息在线通知,离线基本采用极光通知以及集成厂商通道
一、app在线通知
接受到来自于该对话窗口的新消息的时候触发提示音与震动,接收到非当前聊天的新消息时进行消息通知。针对于这种情况,非当前聊天采用本地通知,该对话窗口分别采用原生集成提示音与震动,再回调至ReactNative
二、使用步骤
1.非当前聊天
代码如下(示例):直接进行本地通知
JPush.addLocalNotification({
messageID: data.msgId + '',
title: "标题",
content: "内容",
extras: {"type": "message", message: data},
isLocal: true,
})
2.该对话窗口集成提示音与震动
代码如下(示例):
ios集成
CurrNotice.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
NS_ASSUME_NONNULL_BEGIN
@interface CurrNotice : NSObject<RCTBridgeModule>
@end
NS_ASSUME_NONNULL_END
CurrNotice.m
#import "CurrNotice.h"
#import "AppDelegate.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation CurrNotice
RCT_EXPORT_MODULE();
//当前聊天页面 新消息到达播放声音
RCT_EXPORT_METHOD(setCurrNotification){
SystemSoundID sound = kSystemSoundID_Vibrate;
NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",@"ReceivedMessage",@"caf"];
if (path) {
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&sound);
if (error != kAudioServicesNoError) {
sound = 0;
}
}
AudioServicesPlaySystemSound(sound);//播放声音
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//静音模式下震动
}
@end
当前采用ReceivedMessage收到新消息的铃声,其他铃声可以对照该链接http://iphonedevwiki.net/index.php/AudioServices。
AudioToolbox的引用需要在项目Build Phases— Link Binary With Libraries中引入AudioToolbox.framework 状态为Required
android集成
先在MainApplication中加入MyReactNativePackage
1.MainApplication
public class MainApplication extends Application implements ReactApplication {
public static boolean hasLoadQbsdk =false;
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages =
new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
packages.add(new MyReactNativePackage());
return packages;
}
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}
@Override protected String getJSMainModuleName() {
return "index";
}
};
2.MyReactNativePackage
public class MyReactNativePackage implements ReactPackage {
@Override public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new CurrNoticeModule(reactContext));
return modules;
}
}
3.CurrNoticeModule
public class CurrNoticeModule extends ReactContextBaseJavaModule {
private static final String TAG = "CurrNoticeModule";
private long lastNotifiyTime;
public CurrNoticeModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "CurrNoticeModule";
}
@ReactMethod
public void setCurrNotification() {
AudioManager audioManager = (AudioManager) getCurrentActivity().getSystemService(Context.AUDIO_SERVICE); //此方法是由Context调用的
Vibrator vibrator = (Vibrator) getCurrentActivity().getSystemService(Context.VIBRATOR_SERVICE); //同上
if (System.currentTimeMillis() - lastNotifiyTime < 1000) {
// received new messages within 2 seconds, skip play ringtone
return;
}
try {
lastNotifiyTime = System.currentTimeMillis();
// check if in silent mode
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
Log.e(TAG, "已经调成静音");
return;
}
long[] pattern = new long[]{0, 180, 80, 120};
vibrator.vibrate(pattern, -1); //震动
Ringtone ringtone = null;
if (ringtone == null) {
Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(getCurrentActivity(), notificationUri);
if (ringtone == null) {
Log.d(TAG, "cant find ringtone at:" + notificationUri.getPath());
return;
}
}
if (!ringtone.isPlaying()) {
String vendor = Build.MANUFACTURER;
ringtone.play();
// for samsung S3, we meet a bug that the phone will
// continue ringtone without stop
// so add below special handler to stop it after 3s if
// needed
if (vendor != null && vendor.toLowerCase().contains("samsung")) {
Ringtone finalRingtone = ringtone;
Thread ctlThread = new Thread() {
public void run() {
try {
Thread.sleep(3000);
if (finalRingtone.isPlaying()) {
finalRingtone.stop();
}
} catch (Exception e) {
}
}
};
ctlThread.run();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后在ReactNtaive中通过 NativeModules.CurrNoticeModule.setCurrNotification() 进行调用
总结
以上就是今天要讲的内容,本文主要通过原生集成提示音与震动的功能,再通过RN进行调用集成