notification.js 3.1 KB

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