Difference between revisions of "Push Notifications"
| (17 intermediate revisions by 2 users not shown) | |||
| Line 9: | Line 9: | ||
=== Setup a new project in firebase === | === Setup a new project in firebase === | ||
https://firebase.google.com/docs/web/setup | |||
https://firebase.google.com/docs/android/setup | https://firebase.google.com/docs/android/setup | ||
https://learn.buildfire.com/en/articles/2060582-how-to-set-up-your-firebase-certificates-for-ios-and-android | |||
Create apps for ios, android and web as required | Create apps for ios, android and web as required | ||
| Line 17: | Line 21: | ||
Add the following plugin into the config.xml in the App folder | Add the following plugin into the config.xml in the App folder | ||
In the <platform name="android">section you will need to reference the icon colours for notifications | |||
<config-file parent="/manifest/application/" target="app/src/main/AndroidManifest.xml"> | |||
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/notification_icon" /> | |||
<meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/defaultNotificationColor" /> | |||
</config-file> | |||
<plugin name="cordova-plugin-firebase-messaging" spec="~4.3.1"> | <plugin name="cordova-plugin-firebase-messaging" spec="~4.3.1"> | ||
| Line 25: | Line 37: | ||
</plugin> | </plugin> | ||
see the official plugin page for more info | |||
https://www.npmjs.com/package/cordova-plugin-firebase-messaging | |||
=== Get the firebase config files === | === Get the firebase config files === | ||
| Line 61: | Line 77: | ||
=== Add a UI handler for the messaging === | === Add a UI handler for the messaging === | ||
Install the push notifications UI module: | |||
<syntaxhighlight lang="bash"> | |||
socialstack install UI/PushNotifications | |||
</syntaxhighlight> | |||
This module has the main job of registering for push notifications when the device is either in a native app or via the web. A device on the web will see the permission request for push notifications. | |||
You'll need to edit the PushNotifications.js file to include the correct firebase config from the web application. This config is primarily used when your site is running from the web. | |||
=== Add a settings page to your local wep app === | |||
This will allow you to test the token refresh process in a webapp, and quickly confirm that all looks ok | |||
You need to add a page with the following component | |||
UI/PushSettings | UI/PushSettings | ||
{\n\t\"c\": {\n\t\t\"t\": \"UI/PushSettings\",\n\t\t\"d\": {},\n\t\t\"i\": 2\n\t},\n\t\"i\": 3\n} | |||
Once added, when you visit the page it should try and get a token from firebase and then send an update to the community via /api.ashx/v2/mwa/user/device/add.json | |||
This will allow | This will add a device entry to the community and allow notifications to be passed back to the web app | ||
There are some console log entries to confirm it has been added | |||
=== First Test Time To Get The Client Key === | === First Test Time To Get The Client Key === | ||
| Line 93: | Line 120: | ||
web | web | ||
=== Backend Authentication === | === Community Backend Authentication === | ||
Create a service account via https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?supportedpurview=project | Create a service account via https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?supportedpurview=project | ||
| Line 103: | Line 130: | ||
Click on keys | Click on keys | ||
Create a new key (export as json) and copy | Create a new key (export as json) and copy the contents into the mwa core config -> messaging | ||
The key should look something like | The key should look something like | ||
<syntaxhighlight lang="json"> | |||
< | { | ||
"type": "service_account", | "type": "service_account", | ||
"project_id": "com-theintelligence-app", | "project_id": "com-theintelligence-app", | ||
| Line 118: | Line 145: | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-jgnr7%40com-theintelligence-app.iam.gserviceaccount.com" | "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-jgnr7%40com-theintelligence-app.iam.gserviceaccount.com" | ||
</ | ? | ||
</syntaxhighlight> | |||
=== Community Backend Testing === | |||
Once all is setup and the app is running, it should register itself with the community for push notifications | |||
You can check this by running the following in the editor sandbox on the community, passing in the relevant userid | |||
<syntaxhighlight> | |||
$mwa_v1_push.List({userid}) | |||
</syntaxhighlight> | |||
You should get a list of the devices/browsers linked to the user (y4es browser as running as ss in browser will also register) | |||
e.g. | |||
<syntaxhighlight> | |||
[List-of-UserDevice] | |||
[0] [UserDevice] | |||
DeviceId: "30320502008403" [String] | |||
NotificationKey: "eRLAz76oKlkaclmsOIHjqj:APA91bEhrSbBLADxMLagqucznK5W06dx0JoMxGGwH-XVEZD6kdKdJll7JjVKjHJgpJbc8lSiq_on1j1_t0Puthn7wNKPJpxUnhSNxEVMkBMcq2T_CaZ0YyAGIljgkvp86OC5cLTz1r-j" [String] | |||
NotificationKeyType: "web" [String] | |||
[1] [UserDevice] | |||
DeviceId: "09965002900340" [String] | |||
NotificationKey: "dhC63MgrSyqsi1ilkkwnax:APA91bHuPEAeiFiSHnuTPUs7cQ1J6EP_Fl0x3zxX8jaxqBhQPQKTxOPl72KbH1ZR3-h1_WWI4RzFX2k3RwXcwoX8fNm_rHFm5OOeUGoBV3BkwXB3keVbCXDRWx0AWApxZZoK2x1-78kT" [String] | |||
NotificationKeyType: "firebase" [String] | |||
[2] [UserDevice] | |||
DeviceId: "00000019000" [String] | |||
NotificationKey: "cldvQ2LES5C36bo9QQ52MQ:APA91bElxPKDdbt_rBRJZsMf02JqrK7RdgBfZm1pYCDKcmVSmwYok1exNWpaU6If5B0yTRlB6o7RnHFWybnLF2zNqXvmE6S6_Lb7nfshK0yTB_qCzQFlOsAdwdoLB8L-tNXMgLnp1cj1" [String] | |||
NotificationKeyType: "firebase" [String] | |||
Count: 3 [Int32] | |||
</syntaxhighlight> | |||
In the app itself you can see the device id via the about screen | |||
To send a test message to your device you can use the sandbox again | |||
<syntaxhighlight> | |||
$mwa_v1_push.Test({notificationKey}) | |||
</syntaxhighlight> | |||
=== Backend Integration === | === Backend Integration === | ||
Start by installing the push notifications module: | |||
<syntaxhighlight lang="bash"> | |||
socialstack install Api/PushNotifications | |||
</syntaxhighlight> | |||
This module creates the UserDevice type and also provides the PushNotificationService which can be used to send push notifications to target devices. | |||
Internally the PushNotificationService batches notifications together so can be used for large amounts of push notification traffic too. | |||
=== IOS APNS Key Setup === | === IOS APNS Key Setup === | ||
To access | To access iOS clients you will need to get a key from the Apple developer site and upload it into firebase: | ||
https://learn.buildfire.com/en/articles/5760994-how-to-set-up-your-apple-push-notification-key-for-your-ios-firebase-certificate#:~:text=How%20do%20I%20upload%20my%20APNs%20key%20to,Step%203.%20Click%20on%20Certificates%2C%20IDs%20%26%20Profiles | https://learn.buildfire.com/en/articles/5760994-how-to-set-up-your-apple-push-notification-key-for-your-ios-firebase-certificate#:~:text=How%20do%20I%20upload%20my%20APNs%20key%20to,Step%203.%20Click%20on%20Certificates%2C%20IDs%20%26%20Profiles | ||
Latest revision as of 12:40, 15 July 2024
Setting up firebase
Get the package details from your application
Get the package name from the cordova config.xml
for example <widget android-packageName="com.thermomix.comapp" id="com.thermomix.comapp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
So in this case "com.thermomix.comapp"
Setup a new project in firebase
https://firebase.google.com/docs/web/setup
https://firebase.google.com/docs/android/setup
Create apps for ios, android and web as required
Update your application
Add the following plugin into the config.xml in the App folder
In the <platform name="android">section you will need to reference the icon colours for notifications
<config-file parent="/manifest/application/" target="app/src/main/AndroidManifest.xml">
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/notification_icon" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/defaultNotificationColor" />
</config-file>
<plugin name="cordova-plugin-firebase-messaging" spec="~4.3.1">
<variable name="FIREBASE_MESSAGING_VERSION" value="20.1.+" />
<variable name="ANDROIDX_CORE_VERSION" value="1.0.+" />
</plugin>
see the official plugin page for more info
https://www.npmjs.com/package/cordova-plugin-firebase-messaging
Get the firebase config files
See https://support.google.com/firebase/answer/7015592?hl=en#zippy=%2Cin-this-article for more info but basically
Edit the config.xml in the App folder
within platform name = andriod
<resource-file src="google-services.json" target="app/google-services.json" />
within platform name = ios
<resource-file src="GoogleService-Info.plist" />
Download the 2 config files from the firebase application and copy then into the App folder containing the cordova config
Get a copy of firebase-messaging-sw.js and copy it into the UI/public folder
Pull in the required javascript files
Update the htmlservice config to include
"EndHeadScripts": [
{
"type" : "text/javascript",
"src" : "https://www.gstatic.com/firebasejs/7.14.0/firebase-app.js"
},
{
"type" : "text/javascript",
"src": "https://www.gstatic.com/firebasejs/7.14.0/firebase-messaging.js"
}
],
Add a UI handler for the messaging
Install the push notifications UI module:
socialstack install UI/PushNotifications
This module has the main job of registering for push notifications when the device is either in a native app or via the web. A device on the web will see the permission request for push notifications.
You'll need to edit the PushNotifications.js file to include the correct firebase config from the web application. This config is primarily used when your site is running from the web.
Add a settings page to your local wep app
This will allow you to test the token refresh process in a webapp, and quickly confirm that all looks ok
You need to add a page with the following component
UI/PushSettings {\n\t\"c\": {\n\t\t\"t\": \"UI/PushSettings\",\n\t\t\"d\": {},\n\t\t\"i\": 2\n\t},\n\t\"i\": 3\n}
Once added, when you visit the page it should try and get a token from firebase and then send an update to the community via /api.ashx/v2/mwa/user/device/add.json
This will add a device entry to the community and allow notifications to be passed back to the web app
There are some console log entries to confirm it has been added
First Test Time To Get The Client Key
If all goes well, you should now be able to get a key in PushNotifications.js
see section
.then((currentToken)
You should get some data looking like this, the key can then be used in your backend to send notifications to the web app
notificationKey
xxxxxxxxxxxxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
notificationKeyType
web
Community Backend Authentication
Create a service account via https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?supportedpurview=project
Choose the application
Click on the service account (will initial say no keys under key id)
Click on keys
Create a new key (export as json) and copy the contents into the mwa core config -> messaging
The key should look something like
{
"type": "service_account",
"project_id": "com-theintelligence-app",
"private_key_id": "31dc7cc70581a64dd2fdcb8df2d6179b018a9698",
"private_key": "-----BEGIN PRIVATE KEY-----xxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-jgnr7@com-theintelligence-app.iam.gserviceaccount.com",
"client_id": "108818059171631247824",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-jgnr7%40com-theintelligence-app.iam.gserviceaccount.com"
?
Community Backend Testing
Once all is setup and the app is running, it should register itself with the community for push notifications
You can check this by running the following in the editor sandbox on the community, passing in the relevant userid
$mwa_v1_push.List({userid})You should get a list of the devices/browsers linked to the user (y4es browser as running as ss in browser will also register)
e.g.
[List-of-UserDevice]
[0] [UserDevice]
DeviceId: "30320502008403" [String]
NotificationKey: "eRLAz76oKlkaclmsOIHjqj:APA91bEhrSbBLADxMLagqucznK5W06dx0JoMxGGwH-XVEZD6kdKdJll7JjVKjHJgpJbc8lSiq_on1j1_t0Puthn7wNKPJpxUnhSNxEVMkBMcq2T_CaZ0YyAGIljgkvp86OC5cLTz1r-j" [String]
NotificationKeyType: "web" [String]
[1] [UserDevice]
DeviceId: "09965002900340" [String]
NotificationKey: "dhC63MgrSyqsi1ilkkwnax:APA91bHuPEAeiFiSHnuTPUs7cQ1J6EP_Fl0x3zxX8jaxqBhQPQKTxOPl72KbH1ZR3-h1_WWI4RzFX2k3RwXcwoX8fNm_rHFm5OOeUGoBV3BkwXB3keVbCXDRWx0AWApxZZoK2x1-78kT" [String]
NotificationKeyType: "firebase" [String]
[2] [UserDevice]
DeviceId: "00000019000" [String]
NotificationKey: "cldvQ2LES5C36bo9QQ52MQ:APA91bElxPKDdbt_rBRJZsMf02JqrK7RdgBfZm1pYCDKcmVSmwYok1exNWpaU6If5B0yTRlB6o7RnHFWybnLF2zNqXvmE6S6_Lb7nfshK0yTB_qCzQFlOsAdwdoLB8L-tNXMgLnp1cj1" [String]
NotificationKeyType: "firebase" [String]
Count: 3 [Int32]In the app itself you can see the device id via the about screen
To send a test message to your device you can use the sandbox again
$mwa_v1_push.Test({notificationKey})Backend Integration
Start by installing the push notifications module:
socialstack install Api/PushNotifications
This module creates the UserDevice type and also provides the PushNotificationService which can be used to send push notifications to target devices.
Internally the PushNotificationService batches notifications together so can be used for large amounts of push notification traffic too.
IOS APNS Key Setup
To access iOS clients you will need to get a key from the Apple developer site and upload it into firebase:
References
see the devops projects THE-APP-MCP AND THE-APP-MAP