1、背景
因業務需求,使用RN開發的APP需要支持本地通知,於是研究了一下;本身想找個造好的輪子(react-native-push-notification),但是她對IOS不處理,讓IOS使用PushNotificationIOS。
2、主要代碼
//取消指定的本地通知
static noti_cancelLocalNotifications(userInfo){
if(YrcnApp.Platform.isIOS){
PushNotificationIOS.cancelLocalNotifications(userInfo);
}else{
}
}
//取消所有本地通知
static noti_cancelAllLocalNotifications(){
if(YrcnApp.Platform.isIOS){
PushNotificationIOS.cancelAllLocalNotifications();
}else{
}
}
//檢查本地通知的權限並請求授權
static noti_checkPermissions(succCallback){
if(YrcnApp.Platform.isIOS){
PushNotificationIOS.checkPermissions(function(checkPermissionsObj){
if(checkPermissionsObj.alert == "0"){
PushNotificationIOS.requestPermissions({alert:"1",badge:"1",sound:"1"});
}
succCallback();
});
}else{
}
}
//設置本地通知
static noti_scheduleLocalNotification(obj){
if(YrcnApp.Platform.isIOS){
PushNotificationIOS.scheduleLocalNotification(obj);
}else{
}
}
//獲取本地所有通知數組
static noti_getScheduledLocalNotifications(succCallback){
if(YrcnApp.Platform.isIOS){
PushNotificationIOS.getScheduledLocalNotifications(function(getScheduledLocalNotificationsObj){
succCallback(getScheduledLocalNotificationsObj);
});
}else{
}
}
3、遇到的問題
如何取消指定的通知?
使用PushNotificationIOS.cancelLocalNotifications(userInfo);方法,但是需要本地通知對象的userInfo屬性。
4、生產上的使用
感興趣的朋友可以掃碼下載看看。

