46 lines
1.7 KiB
JavaScript

//
var admin = require('firebase-admin');
// 1. Download a service account key (JSON file) from your Firebase console and add to the example/scripts directory
var serviceAccount = require('./wyatt-notification-example-firebase-adminsdk-aji7z-d74c5eb58d.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
// 2. Copy the token for your device that is printed in the console on app start (`flutter run`) for the FirebaseMessaging example
const token = 'd3gPMU4qfEXLtWuOt0yigw:APA91bF-q1Y36Lz-eb9JrKPR6A7QJHXdwSFN5CN18m3YUQJDD3Km57HWvCjQRFAjA-0vUM507bXQkD4sjI_YmsKyGJsepwViat_ik4_1ra0rh2dv3GVxW9QRr84R_COMG5339IQ3Qitw';
// 3. From your terminal, root to example/scripts directory & run `npm install`.
// 4. Run `npm run send-message` in the example/scripts directory and your app will receive messages in any state; foreground, background, terminated.
// If you find your messages have stopped arriving, it is extremely likely they are being throttled by the platform. iOS in particular
// are aggressive with their throttling policy.
admin
.messaging()
.sendToDevice(
[token],
{
data: {
foo:'bar',
},
notification: {
title: 'A great title',
body: 'Great content',
},
},
{
// Required for background/terminated app state messages on iOS
contentAvailable: true,
// Required for background/terminated app state messages on Android
priority: 'high',
}
)
.then((res) => {
if (res.failureCount) {
console.log('Failed', res.results[0].error);
} else {
console.log('Success');
}
})
.catch((err) => {
console.log('Error:', err);
});