反应本机推送通知无法正常工作 [英] React Native Push Notification Not Working Properly

本文介绍了反应本机推送通知无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

推送通知功能:

PushNotification.configure({
  largeIcon: "ic_launcher",
  smallIcon: "ic_notification",
  onNotification: function (notification) {
    PushNotification.localNotification({
      autoCancel: true,
      message: 'Test Message',
      title: 'Test Message',
      vibrate: true,
      vibration: 300,
      playSound: true,
      soundName: 'default'
    })
    console.log(notification)
  },
});

问题:

  1. 运行应用程序时,如果我从php服务器发送通知,则会在console.log中得到响应

但是

条件1:PushNotification.localNotification()前景不起作用.

condition 1: PushNotification.localNotification() foreground not working.

条件2:PushNotification.localNotification()背景无法正常工作.

condition 2: PushNotification.localNotification() background not working.

  1. 如果我从Firebase服务器发送通知,我会在console.log中得到响应

但是

条件3:PushNotification.localNotification()前景不起作用.

condition 3: PushNotification.localNotification() foreground not working.

条件4:PushNotification.localNotification()后台工作.

condition 4: PushNotification.localNotification() background working.

从Firebase服务器收到第一个通知(即条件4)后,我的应用程序也开始从上述所有其他3个条件接收通知.奇怪,但我测试了很多次

And after I get first notification from firebase server, i.e. condition 4, my application started receiving notifications from all other 3 conditions, mentioned above, also. Strange but I tested many time

  1. 如果我单击通知,则通知仍在栏中显示
  2. 在OnePlus9设备上,总体通知不起作用.

我正在从php服务器获得响应:

I am getting response from php server:

    {
       "data": 
       {
          "message": "Your order with order no OID63112 has been dispatched. Expected time for arrival is 30 min", 
           "title": "Picked up order", 
           "vibrate": "1"
       }, 
       "from": "326331584681", 
       "messageId": "0:1607089521078208%d58aa421f9fd7ecd", 
       "sentTime": 1607089521064, 
       "ttl": 2419200
   }

我正在从Firebase服务器获得响应:

I am getting response from firebase server:

   {   
       "channelId": "fcm_fallback_notification_channel", 
       "color": null, 
       "data": {}, 
       "finish": [Function finish], 
       "foreground": true, "id": "-1787502606", 
       "message": "Enjoy your meat! Order Online!",
       "priority": "high", 
       "sound": null, 
       "tag": "campaign_collapse_key_4040075812614528488", 
       "title": "Freshcut", 
       "userInteraction": false, 
       "visibility": "private"
    }

我的配置是

"@react-native-firebase/app": "^8.4.7",
    "@react-native-firebase/crashlytics": "^8.4.9",
    "@react-native-firebase/database": "^10.0.0",
    "@react-native-firebase/messaging": "^7.9.0",
    "react-native-push-notification": "^6.1.3",

android/build.gradle

android/build.gradle

 ext {
        buildToolsVersion = "29.0.2"
        minSdkVersion = 19
        compileSdkVersion = 29
        targetSdkVersion = 29
        googlePlayServicesVersion = "+" // default: "+"
        firebaseMessagingVersion = "+" // default: "+"
    }

android/app/src/main/AndroidManifest.xml

android/app/src/main/AndroidManifest.xml

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <permission
        android:name="com.freshcut.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.freshcut.permission.C2D_MESSAGE" />

<!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
                    android:value="false"/>
        <!-- Change the resource name to your App's accent color - or any other color you want -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                    android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->
 
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>
 
        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

我无法找到确切的问题,我也阅读了所有其他问题,并尝试了不同的解决方案,但没有解决.我是React-native-puch-notification的新手.

I am not able to get the exact problem, I read all other issues too and tried different solutions but nothing worked out. I am new to react-native-puch-notification.

推荐答案

您必须订阅firebase通知才能接收通知.因此,首先使用消息的 onMessage()方法订阅firebase通知.订阅从Firebase接收到的消息后,请触发localNotification,以在前台弹出更多通知,其中包含您在 remoteMessage 中接收到的标题,消息和其他数据,如下所示:

You have to subscribe the firebase notification to receive notification. So first subscribe firebase notification using onMessage() method of messaging. After subscribing whenever message will receive from firebase, fire the localNotification to pop the notification in foreground more with the title, message and other data that you have received in remoteMessage as below :

useEffect(() => {
  const unsubscribe = messaging().onMessage(async (remoteMessage) => {
    PushNotification.localNotification({
      message: remoteMessage.notification.body,
      title: remoteMessage.notification.title,
      bigPictureUrl: remoteMessage.notification.android.imageUrl,
      userInfo: remoteMessage.data,
    });
  });

  return unsubscribe;
}, []);

这篇关于反应本机推送通知无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆