【问题标题】:react-native-push-notification - local notifications not working on Androidreact-native-push-notification - 本地通知不适用于 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?
    });
  };

【问题讨论】:

  • 我可以查看整个代码文件吗?

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


【解决方案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,则表示该频道已经存在。你也不必调用最后一行,它是可选的。
猜你喜欢
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
相关资源
最近更新 更多