【发布时间】:2020-01-22 19:42:27
【问题描述】:
我正在尝试在 iOS 上的 react-native 应用程序中实现通知,但我遇到了一个奇怪的问题。该应用程序能够显示本地通知,并显示来自 firebase 的远程通知,但它不会按预期触发 react-native-push-notification 的事件“onNotification”。我已经通过许多论坛寻找解决方案,但我仍然没有找到解决方案,这就是我请求您帮助的原因。
经过调查,我意识到objective-c函数“didReceiveLocalNotification”和“didReceiveRemoteNotification”从未被触发,所以我认为这是“onNotification”也没有触发的原因。不幸的是,我完全是这门语言的初学者,所以我无法理解问题的原因。
查看“didReceiveLocalNotification”文档,我了解到该函数已被弃用,我应该使用“willPresentNotification”代替,但与其他函数一样,它从未被触发。
作为记录,我已按照本指南在 iOS 中处理通知: https://firebase.google.com/docs/cloud-messaging/ios/client。我正在 iOS 13、react-native 0.60.5、push-notifiaction-ios ^1.0.2、react-native-push-notification ^3.1.3 和 react-native-firebase ^5.5.6 上进行测试。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
[FIRMessaging messaging].delegate = self;
}
if ([UNUserNotificationCenter class] != nil) {
// iOS 10 or later
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// ...
}];
} else {
// iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
[application registerForRemoteNotifications];
// Load RootView ...
return YES;
}
// sourceURLForBridge function ...
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// Never called
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// Never called
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
// Never called
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
//completionHandler(UNNotificationPresentationOptionAlert);
}
PushNotification 和 firebase 配置
function configurePushNotification(){
PushNotification.configure({
onRegister: function(token) {
console.warn("TOKEN:", token);
},
onNotification: function(notification) {
console.warn("NOTIFICATION:", notification);
// Never called
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true
});
}
async function checkPermission(onReady) {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
console.warn('checkPermission enabled');
getToken(onReady);
} else {
console.warn('checkPermission disabled');
requestPermission(onReady);
}
}
async function requestPermission(onReady) {
try {
await firebase.messaging().requestPermission();
// User has authorised
console.warn('requestPermission try');
getToken(onReady);
} catch (error) {
// User has rejected permissions
console.error(error)
console.warn('requestPermission rejected');
}
}
async function getToken(onReady) {
fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
console.warn(fcmToken)
if(onReady) onReady()
}
class App extends React.Component {
async componentDidMount() {
checkPermission()
configurePushNotification()
}
render(){
return (
// View ...
)
}
};
感谢您的帮助!
编辑:经过调查,我意识到这些事件在我将 Firebase/Messaging 添加到我的 podfile 后停止工作。但根据 firebase 文档,这不是正常行为。关于这个原因的任何想法?
【问题讨论】:
标签: ios objective-c react-native push-notification firebase-cloud-messaging