react-native-push-notification - 本地通知不适用于 Android

Posted

技术标签:

【中文标题】react-native-push-notification - 本地通知不适用于 Android【英文标题】:react-native-push-notification - local notifications not working on Android 【发布时间】:2021-02-21 04:36:23 【问题描述】:

谁能告诉我我哪里出错了。 从我在instructions 上看到的内容来看,如果您只使用本地通知并使用自动链接,则不需要对android 特定文件进行修改......除了将googlePlayServicesVersion = "<Your play services version>" // default: "+" 添加到android/build.gradle....和<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 加上<uses-permission android:name="android.permission.VIBRATE" />AndroidManifest.xml

下面是我的主要源码

PushNotification.configure(
  // (optional) Called when Token is generated (ios and Android)
  onRegister: function(token) 
    console.log('onRegister token:', token);
  ,

  // (required) Called when a remote is received or opened, or local notification is opened
  onNotification: function(notification) 
    console.log('onNotification:', notification);
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  ,

  // Should the initial notification be popped automatically
  popInitialNotification: true,
  requestPermissions: true,
);

  userNowInactive = () => 
          this.showNotification();
 

  showNotification = () => 
    PushNotification.localNotification(
      //ios and android properties
      title: 'Face2Face: Beacon Timer Expired',
      message: 'Perhaps set your beacon timer for another hour?',
      playSound: true,
      soundName: 'sound.mp3',
      //android only properties
      channelId: 'your-channel-id',
      autoCancel: true,
      // largeIcon: 'ic_launcher',
      // smallIcon: 'ic_launcher',
      bigText: 'Face2Face: Beacon Timer Expired',
      subText: 'Perhaps set your beacon timer for another hour?',
      vibrate: true,
      vibration: 300,
      priority: 'high',
      //ios only properties...is there any?
    );
  ;

【问题讨论】:

我可以查看整个代码文件吗? 【参考方案1】:

您是否创建了频道? 看来您需要创建一个才能使其正常工作。

https://github.com/zo0r/react-native-push-notification#channel-management-android

要使用频道,请在启动时创建它们并将匹配的频道 ID 传递给 PushNotification.localNotification 或 PushNotification.localNotificationSchedule。

PushNotification.createChannel(
    
      channelId: "channel-id", // (required)
      channelName: "My channel", // (required)
      channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
      playSound: false, // (optional) default: true
      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.
  );

【讨论】:

是的,谢谢,这是正确的答案。频道必须是 libraby 的最新强制性部分,因为它在“npm install”之前工作。我想教训是每次升级库时都要仔细检查每个库的功能 对我来说它显示 createChannel 返回 false @ShubhamKumar 如果返回 false,则表示该频道已经存在。你也不必调用最后一行,它是可选的。

以上是关于react-native-push-notification - 本地通知不适用于 Android的主要内容,如果未能解决你的问题,请参考以下文章

使用 react-native-push-notification,App 在收到新的 FCM 通知时崩溃

React-native:使用 zo0r/react-native-push-notification 显示前台通知,例如后台通知

react-native-push-notification - 本地通知不适用于 Android

添加 React-Native-Camera 和 React-Native-Push-Notification 后无法构建 React Native

React Native Push Notification在XCode控制台中显示,但不在设备上显示

推送通知 IOS 反应原生 NSDictionary 错误