-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·91 lines (90 loc) · 2.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @format
*/
import "react-native-gesture-handler";
import { AppRegistry } from "react-native";
import App from "./App";
import React from "react";
import { name as appName } from "./app.json";
import TrackPlayer, { Event, State } from "react-native-track-player";
import messaging from "@react-native-firebase/messaging";
import notifee, {
AndroidImportance,
AndroidVisibility,
EventType,
} from "@notifee/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { navigate } from "./src/Routes/RootNavigation";
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log("Message handled in the background!", remoteMessage);
onDisplayNotification(remoteMessage);
sendToAsync(remoteMessage);
});
async function onDisplayNotification(remoteMessage) {
let body = remoteMessage?.notification?.body || "New Notification";
let title =
remoteMessage?.notification?.title ||
"You Have A New Notification Recieved";
let image =
remoteMessage?.notification?.image ||
"http://15.185.185.85:40/assets/img/icon.png";
let data = remoteMessage?.data;
await notifee.requestPermission();
const channelId = await notifee.createChannel({
id: "incoming_task",
name: "Incoming Task",
importance: AndroidImportance.HIGH,
});
await notifee.displayNotification({
title: `${title}`,
body: `${body}`,
data: data,
android: {
largeIcon: `${image}`,
channelId,
importance: AndroidImportance.HIGH,
visibility: AndroidVisibility.PUBLIC,
pressAction: {
launchActivity: "default",
id: "default",
},
},
ios: {},
});
}
async function sendToAsync(remoteMessage) {
let a = await AsyncStorage.getItem("NotData");
if (a) {
let b = JSON.parse(a);
await AsyncStorage.setItem(
"NotData",
JSON.stringify([...b, { ...remoteMessage, read: true }])
);
} else {
let ab = [remoteMessage];
await AsyncStorage.setItem("NotData", JSON.stringify(ab));
}
}
function HeadlessCheck({ isHeadless }) {
if (isHeadless) {
return null;
}
return <App />;
}
notifee.onBackgroundEvent(async ({ type, detail }) => {
const { notification, pressAction } = detail;
if (type === EventType.PRESS) {
console.log("background event", notification, pressAction);
console.log("notification press in bavkground", notification);
if (notification.data.notType == "Event") {
navigate("Home", { screen: "Events" });
} else if (notification.data.notType == "Dua") {
navigate("Home", { screen: "Duas" });
} else if (notification.data.notType == "Conference") {
navigate("Camps");
}
await notifee.cancelNotification(notification.id);
}
});
AppRegistry.registerComponent(appName, () => HeadlessCheck);
TrackPlayer.registerPlaybackService(() => require("./service"));