-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathwithAndroidPushNotifications.ts
More file actions
126 lines (111 loc) · 4.03 KB
/
withAndroidPushNotifications.ts
File metadata and controls
126 lines (111 loc) · 4.03 KB
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
124
125
126
import path from 'path';
import fs from 'fs';
import { type ConfigPlugin, withDangerousMod } from '@expo/config-plugins';
import type { IntercomPluginProps } from './@types';
const SERVICE_CLASS_NAME = 'IntercomFirebaseMessagingService';
function hasExpoNotifications(): boolean {
try {
require('expo-notifications');
return true;
} catch (e: any) {
return e?.code !== 'MODULE_NOT_FOUND';
}
}
/**
* Generates the Kotlin source for the FirebaseMessagingService that
* forwards FCM tokens and Intercom push messages to the Intercom SDK.
*/
function generateFirebaseServiceKotlin(packageName: string): string {
const extendsExpo = hasExpoNotifications();
const baseClass = extendsExpo
? 'ExpoFirebaseMessagingService'
: 'FirebaseMessagingService';
const baseImport = extendsExpo
? 'import expo.modules.notifications.service.ExpoFirebaseMessagingService'
: 'import com.google.firebase.messaging.FirebaseMessagingService';
return `package ${packageName}
${baseImport}
import com.google.firebase.messaging.RemoteMessage
import com.intercom.reactnative.IntercomModule
class ${SERVICE_CLASS_NAME} : ${baseClass}() {
override fun onNewToken(refreshedToken: String) {
IntercomModule.sendTokenToIntercom(application, refreshedToken)
super.onNewToken(refreshedToken)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (IntercomModule.isIntercomPush(remoteMessage)) {
IntercomModule.handleRemotePushMessage(application, remoteMessage)
} else {
super.onMessageReceived(remoteMessage)
}
}
}
`;
}
/**
* Uses withDangerousMod to write the Kotlin FirebaseMessagingService file
* into the app's Android source directory, and ensures firebase-messaging
* is on the app module's compile classpath.
*/
export const withAndroidPushNotifications: ConfigPlugin<IntercomPluginProps> = (
_config
) =>
withDangerousMod(_config, [
'android',
(config) => {
const packageName = config.android?.package;
if (!packageName) {
throw new Error(
'@intercom/intercom-react-native: android.package must be defined in your Expo config to use Android push notifications.'
);
}
const projectRoot = config.modRequest.projectRoot;
const packagePath = packageName.replace(/\./g, '/');
const serviceDir = path.join(
projectRoot,
'android',
'app',
'src',
'main',
'java',
packagePath
);
fs.mkdirSync(serviceDir, { recursive: true });
fs.writeFileSync(
path.join(serviceDir, `${SERVICE_CLASS_NAME}.kt`),
generateFirebaseServiceKotlin(packageName),
'utf-8'
);
// The native module declares firebase-messaging as an `implementation`
// dependency, which keeps it private to the library. Since our generated
// service lives in the app module, we need firebase-messaging on the
// app's compile classpath too. We read the version from the native
// module's build.gradle so it stays in sync automatically.
const packageRoot = path.resolve(__dirname, '..', '..', '..');
const nativeBuildGradle = fs.readFileSync(
path.join(packageRoot, 'android', 'build.gradle'),
'utf-8'
);
const versionMatch = nativeBuildGradle.match(
/com\.google\.firebase:firebase-messaging:([\d.]+)/
);
const firebaseMessagingVersion = versionMatch
? versionMatch[1]
: '24.1.2';
const buildGradlePath = path.join(
projectRoot,
'android',
'app',
'build.gradle'
);
const buildGradle = fs.readFileSync(buildGradlePath, 'utf-8');
if (!buildGradle.includes('firebase-messaging')) {
const updatedBuildGradle = buildGradle.replace(
/dependencies\s*\{/,
`dependencies {\n implementation("com.google.firebase:firebase-messaging:${firebaseMessagingVersion}")`
);
fs.writeFileSync(buildGradlePath, updatedBuildGradle, 'utf-8');
}
return config;
},
]);