首页 > 解决方案 > 反应原生 ios 处理推送通知而不点击通知

问题描述

我正在使用 react-native 0.60,Firebase 推送通知。我需要帮助处理带有数据有效负载的原生 firebase ios 推送通知。我能够发送带有数据有效负载的通知。当应用程序处于前台、后台、被终止时,我能够收到通知。我正在发送带有通知的数据。

    {
      "to" : "FCM Token",
      "show_in_foreground" : "true",
      "collapse_key" : "type_a",
      "notification" : {
      "notification_type" : "N",
      "ad_type" : "banner",
      "sub_type" : "info",
      "text_message" : "Lorem ipsum dolor sit amet",
      "Title": "Notification 0000",
      "image_url": "https://firebasestorage.com/xyz",
      "ad_time" : 7,
      "x_time" : 4,
      "delay_time" : 4
    },
    "data" : {
      "notification_type" : "N",
      "ad_type" : "banner",
      "sub_type" : "info",
      "text_message" : "Lorem ipsum dolor sit amet",
      "Title": "Notification 0000",
      "image_url": "https://firebasestorage.googleapis.com/xyz",
      "ad_time" : 7,
      "x_time" : 4,
      "delay_time" : 4
    },
    "content-available" : true,
    "priority": "high"
  }

即使用户没有单击通知,我也想同时显示通知并将数据存储在本地应用程序数据库中。

当用户单击通知时,我能够处理通知。我想在用户收到通知时更新应用程序中的数据并显示通知。我不想使用静默背景通知。

我的 Appdelegate 代码

   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
     if ([[FIRAuth auth] canHandleNotification:userInfo]) {
        completionHandler(UIBackgroundFetchResultNoData);
        return;
     }
     [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
     [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
     NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background 3");

   }

我想实现 1. 即使应用程序关闭、终止、后台,用户也应该收到通知。2.即使用户没有点击通知,用户的应用本地数据库也应该更新。

我已经达到了寻找第二点的第一点。我是新来的反应本地人。我知道收到通知时会调用完成处理程序方法。当应用程序被杀死时,xcode 日志中不会显示任何内容。我如何在完成处理程序方法中编写代码?以及如何检查是否调用了该方法并执行了任务。

标签: iosreact-nativepush-notificationapple-push-notifications

解决方案


messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate(remoteMessage.data.type);
    });

    // Check whether an initial notification is available 
    // This will handle notification when App is opened from killed state or background
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
        }
        setLoading(false);
      });

使用此通知侦听器

第二件事 React-Native 不能管理应用程序的终止状态。因为当应用程序被杀死时,js引擎就会关闭。

第三:v6 lib 不像 v5 那样支持徽章计数更新。所以当有通知和用户直接打开应用程序时,所有通知都应该被清除,这样就可以从本机端完成

对于 Android -> MainActivity.java

@Override
protected void onResume() {
super.onResume();

// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}

因此,无论是直接打开应用程序还是从 Notification Press 打开应用程序,这都会清除来自 StatusBar、LockScreen 的所有通知。但是,如果要求是这样,仍然这样做。

在更多的事情上,Android 管理徽章计数和自动更新,但对于 iOS 本地代码实现必须。在 AppDelegate 文件中。

最常见的错误:remoteMessage.notification 和 remoteMessage.data 都不同,控制台整个 remoteMessage 对象:)))


推荐阅读