【问题标题】:Schedule local notifications in react native not working在本机反应中安排本地通知不起作用
【发布时间】:2020-11-18 04:28:08
【问题描述】:

我正在使用react-native-push-notifications npm 库在我的应用程序中实现通知功能。但是,我想安排通知,我正在使用PushNotification.localNotificationSchedule 这样做。但不知何故,当我试图触发时它不起作用。我已经粘贴了下面的代码。有人可以帮我解决这个问题,我是这个功能的新手。

事件处理程序

const testPush = () =>{
      PushNotification.localNotificationSchedule({
        title:'My notification title',
        date:new Date(new Date().getTime()+3000),
        message:'My notification Message',
        allowWhileIdle:false
      });
    }

事件触发

      <Pressable
        onPress={testPush}
        style={{ marginTop: 13 }}
      >
        <View style={{ backgroundColor: '#20232a', padding: 13, alignItems: 'center', borderRadius: 10 }}>
          <Text style={{ color: '#ffffff' }}>Test push</Text>
        </View>
      </Pressable>

【问题讨论】:

  • 这是发生在 iOS 还是 Android 上?
  • 你好 Satheesh,它正在 Android 中发生。

标签: javascript react-native npm push-notification react-native-push-notification


【解决方案1】:

您错过了文档中的小东西 (https://github.com/zo0r/react-native-push-notification#readme),因为他们最近更新了它 您必须为此创建频道。 浏览文档 https://github.com/zo0r/react-native-push-notification#channel-management-android 仅适用于 android 并且在 IOS 中你的代码可以正常工作

  PushNotification.createChannel(
{
  channelId: "channel-id", // (required)
  channelName: "My channel", // (required)
  channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
  soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
  importance: 4, // (optional) default: 4. Int value of the Android notification importance
  vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.);

注意:没有频道,通知不起作用

在计划代码中添加以下行

channelId: "你的频道 ID"

添加如下代码

PushNotification.localNotificationSchedule({
    title:'My notification title',
    date:new Date(new Date().getTime()+3000),
    message:'My notification Message',
    allowWhileIdle:false,
    channelId: "your-channel-id"
  });

【讨论】:

  • 或者你可以在AndroidManifest.xml中添加
  • 这不是否定之前的警告“没有频道,通知不起作用....除非您使用默认的通知频道ID”?
  • @ChandhanNarayanareddy 为使您的建议起作用,必须将 fcm_default_channel 添加到他们的 res > values > strings.xml 文件中
【解决方案2】:

对我来说,您的示例代码有效

所以请确保您已安装、授予权限并正确链接。

(a) 检查 android/build.gradle 是否有

buildscript {
    ...
    dependencies {
        ...
        classpath('com.google.gms:google-services:4.3.3')
        ...
    }
}

(b) 签入 android/app/build.gradle

dependencies {
  ...
  implementation 'com.google.firebase:firebase-analytics:17.3.0'
  implementation project(':react-native-push-notification')
  ...
}

apply plugin: 'com.google.gms.google-services'

(c) 签入 MainApplication.java

import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

(d) 签入 android/app/src/main/res/values/colors.xml(如果文件不存在则创建)。

<resources>
    <color name="white">#FFF</color>
</resources>

(e) 签入 android/app/src/main/AndroidManifest.xml

.....
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application ....>
        <!-- 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>
     .....

【讨论】:

  • 我遇到了本地预定通知问题,添加 import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage 这帮助我解决了谢谢
  • 如果您不使用 Firebase,则不需要连接 Firebase
【解决方案3】:

添加import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

为我做了一个技巧,尝试将其添加到您的 MainApplication.java 中

我能够收到 localNotification,但它只是未收到的预定通知。

【讨论】:

  • 谢谢!这对我有用。我想知道他们为什么不将其包含在文档中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
相关资源
最近更新 更多