notification.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import PushNotificationIOS from "@react-native-community/push-notification-ios";
  2. import PushNotification, { Importance } from "react-native-push-notification";
  3. global.notifyToken = {};
  4. // Must be outside of any component LifeCycle (such as `componentDidMount`).
  5. PushNotification.configure({
  6. //(可选)生成Token时调用(iOS和Android)
  7. onRegister: function (token) {
  8. console.log("TOKEN:", token);
  9. global.notifyToken = token;
  10. },
  11. // (必需)在收到或打开远程通知或打开本地通知时调用
  12. onNotification: function (notification) {
  13. console.log("NOTIFICATION:", notification);
  14. if (notification.title) {
  15. //TODO 处理通知
  16. notification.channelId = "10086";
  17. notification.smallIcon = "ic_notification";
  18. notification.showWhen = true;
  19. PushNotification.localNotification(notification);
  20. } else if (notification.data) {
  21. //TODO 处理消息
  22. }
  23. //(必需)在收到或打开远程通知或打开本地通知时调用
  24. if (notification.finish) {
  25. notification.finish(PushNotificationIOS.FetchResult.NoData);
  26. }
  27. },
  28. // (可选)当按下Registered Action且invokeApp为false时调用,如果为true,将调用onNotification(Android)
  29. onAction: function (notification) {
  30. console.log("ACTION:", notification.action);
  31. console.log("NOTIFICATION:", notification);
  32. // process the action
  33. },
  34. //(可选)当用户注册远程通知失败时调用。通常发生在APN出现问题时,或者设备是模拟器时(操作系统)
  35. onRegistrationError: function(err) {
  36. console.info(err.message, err);
  37. },
  38. // 仅限IOS(可选):默认值:all - Permissions to register.
  39. permissions: {
  40. alert: true,
  41. badge: true,
  42. sound: true,
  43. },
  44. // 初始通知自动弹出
  45. // default: true
  46. popInitialNotification: true,
  47. /**
  48. *(可选)默认值:true
  49. *-指定是否请求权限(ios)和令牌(android和ios),
  50. *-否则,必须稍后调用PushNotificationsHandler.requestPermissions()
  51. *-如果未使用远程通知或未安装Firebase,请使用以下命令:
  52. * requestPermissions:Platform.OS==='ios'
  53. */
  54. requestPermissions: true,
  55. });
  56. PushNotification.createChannel(
  57. {
  58. channelId: "10086", // (required)
  59. channelName: "Juice+ Messaging", // (required)
  60. channelDescription: "Juice+ notification center, remind of the charging process", // (optional) default: undefined.
  61. playSound: false, // (optional) default: true
  62. soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
  63. importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
  64. vibrate: true, // (optional) default: true. Creates the default vibration pattern if true.
  65. },
  66. //(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  67. );
  68. /*PushNotification.getChannels(function (channel_ids) {
  69. console.log('channel_ids', channel_ids); // ['channel_id_1']
  70. });*/