跨平台设置Android和iOS平台应用角标
$ npm install react-native-wayne-badge --save
or
$ yarn add react-native-wayne-badge
ios端
pod install
// 导入包
import RNWayneBadge from 'react-native-wayne-badge';
// 使用包
RNWayneBadge.setBadge(1);
private void setupNotificationChannel() {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = null;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("com.chengfenghuoyun.crm", "notification_channel",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
notification = new Notification.Builder(getApplicationContext(), "com.chengfenghuoyun.crm")
.setContentTitle("")
.setContentText("")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
} else {
notification = new Notification.Builder(getApplicationContext())
.setContentTitle("")
.setContentText("")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
}
mNotificationManager.notify(1, notification);
}
在AppDelegate.m文件中
1. 导入UNUserNotificationCenter
#import <UserNotifications/UNUserNotificationCenter.h>
2. 在AppDelegate中引用
@interface AppDelegate () <RCTBridgeDelegate, UNUserNotificationCenterDelegate>
3. 申请通知权限
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];