【问题标题】:React Native Android: Detect tap on a notificationReact Native Android:检测点击通知
【发布时间】:2020-04-20 09:40:55
【问题描述】:

我正在尝试检测对通知的点击。

我使用 react-native 0.61 和:react-native-notifications 3.1.4(但使用 react-native-push-notification 3.1.9 得到相同的结果)。

一切正常,但只有一件事:如果我点击通知,或者如果我从带有 Firebase 管理员通知的通知中打开我的应用程序(或 curl 到 FCM),我不会触发这些事件:

Notifications.events().registerNotificationOpened

Notifications.getInitialNotification()

但是,如果我使用 Notifications.postLocalNotification 发布一个简单的本地通知,一切正常。

这是我的标准 curl 请求:

curl -X POST \
  https://fcm.googleapis.com/fcm/send \
  -H 'Authorization: key=mykey' \
  -H 'Content-Type: application/json' \
  -H 'Host: fcm.googleapis.com' \
  -d '{
 "to" : "cgh6P5BESPesyBJwYlaSPi:APA91bEuLwxgZq4JUlo2Su-_l1DqNVoAPzC2eHQy07IQlTuUcZNRuFiLzlbiFuCsHhU74SgYbrIkY7e2v4uQbYCspzRLlDrwrfha40Ozv613xwyYPtR3lJMaTuNcNAhGh8bdrmMY4B3N",
 "payload" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title", 
     "pushNotification": true,   
 }

再次出现通知,但如果我点击它,我不会触发任何事件。但是,如果我使用 postLocalNotification 我会收到这些事件。

我已经为此苦苦挣扎了好几天,我是否必须在我的AndroidManifest 中对intents 做点什么?有这个工作的示例项目吗?

【问题讨论】:

    标签: android react-native push-notification react-native-push-notification react-native-notifications


    【解决方案1】:

    经过几天的努力,问题出在AndroidManifest 内部,我有两个活动,一个用于启动屏幕,另一个用于通知。

    我的 AndroidManifest 现在是这样的:

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.INFO" />
      </intent-filter>
    </activity>
    <activity
      android:name=".SplashActivity"
      android:theme="@style/SplashTheme">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    

    【讨论】:

    • 我尝试添加启动活动。我收到以下错误:“找不到资源样式/SplashTheme(又名 com.myApp:style/SplashTheme)。”有什么建议吗?
    • 您可能不使用启动画面,因此您不需要添加此 sn-p 代码
    • 嘿@Simon,我为你这几天遇到的问题而苦苦挣扎。我想知道清单文件中的这段代码是如何让它为你工作的。询问是因为我在文件中找到的默认代码与您共享的完全相同! :D (虽然细微的差别不是 INFO,但我有 LAUNCHER,但由于我没有使用启动画面,我认为这是正确的)那么,你到底做了什么改变,这对你有什么帮助?
    【解决方案2】:

    1) Notifications.events().registerNotificationOpened, Notifications.events().registerNotificationReceivedBackground 如果您使用启动画面,则不会触发 Notifications.getInitialNotification()。 2) 对于通知上的自定义图标和颜色,将图标添加到所有 res/mipmap- 然后使用元数据标签。 像这样修改AndroidManifest来解决上述问题:*

    <uses-permission android:name="android.permission.INTERNET" />
    
    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
    
      <meta-data  
      android:name="com.google.firebase.messaging.default_notification_icon" 
      android:resource="@mipmap/ic_notifications" />
      <meta-data
      android:name="com.google.firebase.messaging.default_notification_color"
      android:resource="@color/app_bg" />
    
      <!-- Add this SplashActivity -->
      <activity
        android:name=".SplashActivity"
        android:theme="@style/SplashTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize" >
          <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.INFO" />
          </intent-filter>
        </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多