I am using Realm for an already quite elaborate application and I want to finish it by adding push notifications. I have already installed react-native-push-notification and make it works with FCM (Firebase Cloud Messaging).
On the client side, I do receive test notifications from Firebase.
I then configured the push notifications in the Realm console by adding the Sender Id and the API Key.
The problem is that when I send a notification from Realm it ends up in "sent", but absolutely nothing happens, no logs in the Realm console, nothing on the Firebase side and nothing on the client side.
The documentation on push notifications for Realm is really limited and I can't quite figure out what to do. The documentation is mainly directed for IOS and Android.
That's my code in index.js:
import PushNotificationIOS from '#react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';
PushNotification.configure({
onRegister: function (token) {
console.log('TOKEN:', token);
},
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
onAction: function (notification) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
},
onRegistrationError: function (err) {
console.error(err.message, err);
},
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
});
PushNotification.createChannel(
{
channelId: 'fcm_fallback_notification_channel',
channelName: 'FCM CHANNEL',
channelDescription: 'Description test',
},
created => console.log(`CreateChannel returned '${created}'`),
);
PushNotification.localNotification({
channelId: 'fcm_fallback_notification_channel',
vibrate: true,
vibration: 300,
playSound: true,
soundName: 'default',
});
Does anyone know of a tutorial or something that could give me some additional information?
So to make it work, you'll have to add this line in index.js:
PushNotification.subscribeToTopic('topic-id-in-realm-console');
And use the same topic id in Realm console:
It ends up with something like this (index.js):
import PushNotificationIOS from '#react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';
PushNotification.configure({
onRegister: function (token) {
console.log('TOKEN:', token);
},
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
onAction: function (notification) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
},
onRegistrationError: function (err) {
console.error(err.message, err);
},
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
});
PushNotification.subscribeToTopic('topic-id-in-realm-console');// <== HERE
On client:
Related
I'm trying to perform a push notification for Google Actions Intent.
Thus far, I've followed the instructions here: https://developers.google.com/actions/assistant/updates/notifications#send_notifications
This is my resulting code:
const {google} = require('googleapis');
var request = require('request');
const key = require('./bot.json');
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
let jwtClient = new google.auth.JWT(
key.client_email, null, key.private_key,
['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
null
);
jwtClient.authorize((err, tokens) => {
// code to retrieve target userId and intent
let notif = {
userNotification: {
title: [message],
},
target: {
userId:[obtained from permission request],
intent: [name of intent],
// Expects a IETF BCP-47 language code (i.e. en-US)
locale: 'en-US'
},
};
request.post('https://actions.googleapis.com/v2/conversations:send', {
'auth': {
'bearer': tokens.access_token,
},
'json': true,
'body': {'customPushMessage': notif},
}, (err, httpResponse, body) => {
console.log(body);
console.log(httpResponse.statusCode + ': ' + httpResponse.statusMessage);
});
});
};
//module.exports(console, "Test");
This results in a 403 from the notification service. Is this because of the user id, intent name or jwtoken that was generated?
Following are the steps we need to check before sending the push notification
Check your Google permission settings
In order to test the Action, you need to enable the necessary permissions.
Go to the ‘Activity Controls' page (https://myaccount.google.com/activitycontrols).
Sign in with your Google account, if you have not already done so.
Ensure that the following permissions are enabled:
a.Web & App Activity
b.Device Information
c.Voice & Audio Activity
2.Target intent name should be added into the Implicit invocation field.
with enabled push notification.
3.use the same email id in your google assistant which you had used for login in GCP.
I am using firebase cloud functions to send a notification to a particular user. This is the payload that I am sending from the functions.
var payload = {
notification: {
sound: "default",
color: "#ff3296fa",
vibrate: "300",
priority: 'high',
notificationType: "52",
title: titleToBeShown,
body: message['message'],
icon: 'ic_launcher',
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
channelId: channelID,
channelName: channelName,
channelType: channelType
},
};
I am using firebase_messaging (flutter package: https://pub.dartlang.org/packages/firebase_messaging) to receive the notifications and I have written the codes for onMessage, onLaunch and onResume methods.
So when I send a message using Admin SDK admin.messaging().sendToDevice(token, payload), it sends it without vibration and sound. How can I add vibration and sound to it? Right now, it feels like a silent notification then. Which will easily be ignored by the users. Both in android and ios, its the same problem.
The sound field does not go in the notification object. It belongs in the android and apns objects. Your payload should look like this:
var payload = {
data: {
channelId: channelID,
channelName: channelName,
channelType: channelType
},
android: {
priority: 'high',
notification: {
title: titleToBeShown,
body: message['message'],
icon: 'ic_launcher',
sound: 'default',
color: '#ff3296fa',
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
// Not sure what this is supposed to be, but not a valid parameter
notificationType: '52',
},
},
apns: { ... }
};
I've filled in the Android fields, but I'm not familiar with APNS payloads. Check out the FCM documentation here for more details and you can see the available payload options for APNS here.
I had setup push notification in my App using the following command:
ionic cordova plugin add phonegap-plugin-push --variable SENDER_ID="1234567890"
Which added the below code to my package.json
"phonegap-plugin-push": {
"SENDER_ID": "1234567890"
}
Now the issue is I have to change this sender ID to something else and I want to know how to do this, also I have the below code in my project to set up the options for push notification.
const options: PushOptions = {
android: {
senderID: '218580369028'
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
Is this as simple as just replacing the sender ID with what I want in package.json and in the above code. Or do I have to do something else for this. Any help is greatly appreciated.
I'm using ionic's cloud push notification service for both android and iOS, along with phonegap-plugin-push. Since i'm using my own server-side authentcation and not ionic's, I have it configured so that a user's authentication token is registered depending on whether or not they're logged in, and saved when they log in (unregistered when they log out).
App.module:
const cloudSettings: CloudSettings = {
'core': {
'app_id': 'APP_ID',
},
'push': {
'sender_id': 'SENDER_ID',
'pluginConfig': {
'ios': {
'badge': true,
'sound': true,
},
'android': {
'iconColor': '#343434'
}
}
}
};
instead of making push a provider in app module, I made an injectable service that can be used across components. The main app component subscribes to the notifications, while my login and logout components tell it whether or not to register and save them.
export class NotificationService{
token: PushToken;
registered: boolean;
constructor(public push: Push){
}
checkLogin(status){
if(!status){
this.push.register().then((pushT: PushToken) => {
this.token = pushT;
console.log('token saved:'+ this.token);
});
}
}
saveToken(){
this.push.saveToken(this.token);
console.log('token saved');
this.registered = true;
this.startListening();
}
deleteToken(status){
if(status){
this.push.unregister().then(function(){
console.log('token deleted');
return true;
})
}
}
startListening(){
if(this.registered){
return this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
});
}}
getToken(){
return this.token.token;
}
}
I have my sender ID for GCM configured in config.xml and ionic cloud, and my APNS certificate is saved in ionic cloud. So far this works perfectly on android. On iOS it registers the token and saves to the database, but testing on Ionic cloud my iPhone doesn't receive any notifications. Any thoughts?
I have implemented ionic push notifications using pushwoosh. But there I use the dashboard which has provided by pushwoosh to send push notifications. I wanted to send push notifications using a back end which has created by my self. How do I do that?
i am working on ionic-framework and also using push notification to sent notification via REST API using node-gcm its working fine.
sample code
(function() {
function execute(rqst, q, fwk) {
var gcm = require('node-gcm');
var message = new gcm.Message();
message.timeToLive = 3000;
message.addData({
title: 'Push Notification Sample',
body: 'Abnormal data access',
icon: 'ic_launcher',
message: '"\u270C Peace, Love \u2764 and PhoneGap \u2706!'
});
message.addNotification({
title: 'Push Notification Sample',
body: 'Abnormal data access',
icon: 'ic_launcher',
message: 'hey , how are you?'
});
/* message.addData('message', "\u270C Peace, Love \u2764 and PhoneGap \u2706!");
message.addData('title', 'Push Notification Sample');
message.addData('msgcnt', '3');*/
// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
// Set up the sender with you API key
var sender = new gcm.Sender('xxxxxxxxxxxxxxxxx-hK5wE');
// Add the registration IDs of the devices you want to send to
var registrationIds = [];
registrationIds.push('APA91bEWB6-xcrfrfrffr-LqyMohLP4T-XuydQgt44Q6Acw5kmVDWvAaOsm1CriASm02SyBceZ2NBWF4FIES7grcPeY5v4fLQme2UqhRteeWRdD_Ma25QMGESOGAyw_Uhgg_EjkTl-');
// Send the message
// ... trying only once sendNoRetry
sender.send(message, {
registrationIds: registrationIds
}, function(err, result) {
if (err) {
console.error(err);
q.resolve({
status: 200,
data: {
data: err,
code: 1
}
});
} else {
console.log(result);
q.resolve({
status: 200,
data: result
});
}
});
}
return exports.execute = execute;
})();