66// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77// Other code you write will be lost the next time you deploy the project.
88import { NativeModules , Platform } from "react-native" ;
9- import PushNotification , { PushNotificationObject } from "react-native-push-notification " ;
9+ import notifee , { AndroidChannel , AndroidImportance , Notification } from "@notifee/ react-native" ;
1010
1111// BEGIN EXTRA CODE
1212// END EXTRA CODE
@@ -32,59 +32,54 @@ export async function DisplayNotification(
3232 actionGuid ?: string
3333) : Promise < void > {
3434 // BEGIN USER CODE
35- // Documentation https://github.com/zo0r/react-native-push-notification
36-
37- const isIOS = Platform . OS === "ios" ;
38- if ( NativeModules && isIOS && ! NativeModules . RNCPushNotificationIOS ) {
39- return Promise . reject ( new Error ( "Notifications module is not available in your app" ) ) ;
35+ if ( ! body ) {
36+ throw new Error ( "Input parameter 'Body' is required" ) ;
4037 }
4138
42- if ( ! body ) {
43- return Promise . reject ( new Error ( "Input parameter 'Body' is required" ) ) ;
39+ // Documentation Documentation https://github.com/invertase/notifee
40+ if ( NativeModules && ! NativeModules . NotifeeApiModule ) {
41+ return Promise . reject ( new Error ( "Notifee native module is not available in your app" ) ) ;
4442 }
4543
46- const notification = { message : body } as PushNotificationObject ;
44+ const channelId = playSound ? "mendix-local-notifications-withsound" : "mendix-local-notifications" ;
45+ await createNotificationChannelIfNeeded ( channelId ) ;
4746
48- if ( ! isIOS ) {
49- const channelId = "mendix-local-notifications" ;
50- const channelExists = await new Promise ( resolve =>
51- PushNotification . channelExists ( channelId , ( exists : boolean ) => resolve ( exists ) )
52- ) ;
53- if ( ! channelExists ) {
54- const channel = await new Promise ( resolve =>
55- PushNotification . createChannel (
56- {
57- channelId,
58- channelName : "Local notifications"
59- } ,
60- created => resolve ( created )
61- )
62- ) ;
63- if ( ! channel ) {
64- return Promise . reject ( new Error ( "Could not create the local notifications channel" ) ) ;
65- }
66- }
67- notification . channelId = channelId ;
68- }
47+ const notification : Notification = {
48+ title : title || undefined ,
49+ body,
50+ android : { channelId, sound : "default" } ,
51+ ios : playSound ? { sound : "default" } : { }
52+ } ;
6953
70- if ( title ) {
71- notification . title = title ;
54+ if ( subtitle && Platform . OS === "ios" ) {
55+ notification . subtitle = subtitle ;
7256 }
7357
74- if ( subtitle && ! isIOS ) {
75- notification . subText = subtitle ;
58+ if ( actionName || actionGuid ) {
59+ notification . data = {
60+ actionName : actionName ?? "" ,
61+ guid : actionGuid ?? ""
62+ } ;
7663 }
7764
78- notification . playSound = ! ! playSound ;
65+ await notifee . displayNotification ( notification ) ;
7966
80- if ( actionName || actionGuid ) {
81- notification . userInfo = {
82- actionName,
83- guid : actionGuid
67+ async function createNotificationChannelIfNeeded ( channelId : string ) : Promise < void > {
68+ if ( Platform . OS === "ios" ) {
69+ return ;
70+ }
71+ const existingChannel = await notifee . getChannel ( channelId ) ;
72+ const channel : AndroidChannel = {
73+ id : channelId ,
74+ name : "Local Notifications" ,
75+ importance : AndroidImportance . HIGH ,
76+ ...( playSound ? { sound : "default" } : { } )
8477 } ;
78+ if ( existingChannel === null ) {
79+ await notifee . createChannel ( channel ) ;
80+ }
8581 }
8682
87- PushNotification . localNotification ( notification ) ;
8883 return Promise . resolve ( ) ;
8984 // END USER CODE
9085}
0 commit comments