-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (110 loc) · 5.45 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {AppRegistry, Linking} from 'react-native';
import App from './App';
import PushNotification from 'react-native-push-notification';
/**
* @format
*/
import React from 'react';
import {name as appName} from './app.json';
import messaging from '@react-native-firebase/messaging';
async function onMessageReceived(message) {
const {title, body, image, tag, router} = await message.data;
console.log('background message: ' + JSON.stringify(message.data));
PushNotification.createChannel({
channelId: 'Twitchifier-Channel', // (required)
channelName: 'Twitchifier Channel', // (required)
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.
});
PushNotification.localNotification({
/* Android Only Properties */
channelId: 'Twitchifier-Channel', // (required) channelId, if the channel doesn't exist, it will be created with options passed above (importance, vibration, sound). Once the channel is created, the channel will not be update. Make sure your channelId is different if you change these options. If you have created a custom channel, it will apply options of the channel.
smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher". Use "" for default small icon.
bigText: title, // (optional) default: "message" prop
subText: body, // (optional) default: none
bigPictureUrl: image, // (optional) default: undefined
bigLargeIcon: 'ic_launcher', // (optional) default: undefined // (optional) default: undefined
color: '#16e6b4', // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
userInfo: {router: router},
priority: 'high', // (optional) set notification priority, default: high
visibility: 'public', // (optional) set notification visibility, default: private
ignoreInForeground: false, // (optional) if true, the notification will not be visible when the app is in the foreground (useful for parity with how iOS notifications appear). should be used in combine with `com.dieam.reactnativepushnotification.notification_foreground` setting
onlyAlertOnce: false, // (optional) alert will open only once with sound and notify, default: false
tag,
invokeApp: false,
id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
title: title, // (optional)
message: body, // (required)
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
});
}
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token) {
console.log('TOKEN:', token);
},
// (required) Called when a remote is received or opened, or local notification is opened
onNotification: function (notification) {
console.log('Notification:', notification);
if (notification.tag == 'twitch') {
Linking.canOpenURL(`twitch://stream/${notification.data.router}`).then(
(supported) => {
if (supported) {
Linking.openURL(`twitch://stream/${notification.data.router}`);
} else {
Linking.openURL(
`https://www.twitch.tv/${notification.data.router}`,
);
}
},
);
} else if (notification.tag == 'youtube') {
Linking.canOpenURL(`vnd.youtube://${notification.data.router}`).then(
(supported) => {
if (supported) {
Linking.openURL(`vnd.youtube://${notification.data.router}`);
} else {
Linking.openURL(
`https://www.youtube.com/watch?v=${notification.data.router}`,
);
}
},
);
} else if (notification.tag == 'twitter') {
Linking.canOpenURL(
`twitter://status?id=${notification.data.router}`,
).then((supported) => {
if (supported) {
Linking.openURL(`twitter://status?id=${notification.data.router}`);
} else {
let publisher = notification.title.split(' ')[0];
Linking.openURL(
`https://twitter.com/${publisher}/status/${notification.data.router}`,
);
}
});
}
// process the notification
// (required) Called when a remote is received or opened, or local notification is opened
notification.finish(PushNotification.FetchResult);
},
// (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
onAction: function (notification) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
// process the action
},
});
messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);
function HeadlessCheck({isHeadless}) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
AppRegistry.registerComponent(appName, () => HeadlessCheck);