Message not delivering in GCM - android

I am using this Java code for sending message
Sender sender = new Sender("AIza..................Q");
// use this to send message with payload data
Message message = new Message.Builder()
.collapseKey("message")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message", "Welcome to Push Notifications") //you can get this message on client side app
.build();
//Use this code to send notification message to a single device
Result result = sender.send(message,
"eBXXXXX:XXXXXXXXXXX",
1);
I am getting response
[ messageId=0:1465322271594526%5036bc7038eb0007 ]
On my mobile I have used
var push = PushNotification.init({
android: {
senderID: "SNDER_ID"
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});
push.on('registration', function(data) {
console.log("registration: "+JSON.stringify(data));
});
push.on('notification', function(data) {
console.log("notification: "+JSON.stringify(data));
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});
push.on('error', function(e) {
console.log("error: "+JSON.stringify(e));
});
Here I am using this plugin but I am not getting any message inside notification

Related

How to correctly specify channel_id in FCM using cloud functions

Unfortunately, no answer yet. I am checking out postman, I hope I can use that to test quicker.
I manage to send a notification through my app, however, the notification always ends up in the silent notification of my phone, no sound, no vibration and no notification icon in the top left of the phone, only a notification in the drawer when I swipe down :(
In an attempt to fix / improve the situation I tried the following:
Create an android notification channel with id: high_importance_channel by using flutter_local_notifications package. The channel was created successful, because requesting an overview of the existing channels, showed the newly created channel (among the other channels). The new channel has importance: 5, enableVibration: true and playSound: true, so that should do the job.
Send a FCM through cloud functions with the following code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.chatNotification = functions.firestore
.document('chats/{groupId}/chat/{chatId}')
.onCreate( async (snapshot, context) => {
const message = {
"notification": {
"title": "Message Received",
"body": "Text Message from " + fromUserName,
},
"tokens": registrationTokens,
"android": {
"notification": {
"channel_id": "high_importance_channel",
},
},
};
admin.messaging().sendMulticast(message)
.then((response) => {
console.log(response.successCount + ' messages were sent successfully');
});
}
But so far not luck, the notification still ends up in the silent notifications. What am I doing wrong?
There are 2 ways of doing it. Might work in your case.
Way 1:
var payload = {
notification: {
android_channel_id: 'AppChannel',
/*
https://stackoverflow.com/questions/62663537/how-do-i-add-a-channelid-to-my-notification
https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
*/
title: 'Push Notification Arrived!',
body: 'Using Cloud Functions',
sound: 'default',
},
data: {
route: '/someRoute',
},
};
try {
const response = await admin.messaging().
sendToDevice(deviceTokens, payload);
console.log('Notification sent succesfully.');
} catch (err) {
console.log('Error sending Notifications...');
console.log(err);
}
Way 2:
const message = {
/*
https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.basemessage.md#basemessagenotification
*/
notification: {
title: 'Push Notification Arrived!',
body: 'Using Cloud Functions',
},
data: {
route: '/someRoute',
},
android: {
/*
https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.androidnotification.md#androidnotificationbodylockey
*/
notification: {
channelId: "AppChannel",
priority: 'max',
defaultSound: 'true',
},
},
token: deviceTokens,
};
// https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message, for LOVEs sake:', error);
});

FCM Status bar icon not showing

I am trying to add a white icon for push notifications sent via FCM admin. Per the guides here, I should be able to just add in the "icon" key and its corresponding URL to the notification. However, it is not working. My default app Icon gets used instead when the notification is received on my phone. Any idea why it's not working? Here is my code
var message = {
notification: {
title: "title here",
body: "body message here"
},
android: {
notification: {
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "https://firebasestorage.googleapis.com/xxxxx"
}
},
data: {
tab: "message",
subsection: "notification"
},
token: registrationToken
};
// send the push notification
var sentMessage = admin.messaging().send(message)
.then(function (response) {
console.log("Successfully sent message: ", response);
})
.catch(function (error) {
console.log("Error sending message: ", error);
});

Application stops When receive a notification from Firebase Notifications

My application stops when you receive a notification, when the app is open, but when the application is not active, it receives 2 notifications.
Here is my initPushNotification():
initPushNotification(){
if(!this.platform.is('cordova')){
console.warn("Push notifications not initialized. Cordova is not available - Run in physical device");
return;
}
const options: PushOptions = {
android: {
senderID:"XXXXXXXXXXXX",
vibrate: true
},
ios: {
alert: "true",
badge: false,
sound: "true"
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((data:any)=>{
console.log("device token", data.registrationId);
//Here I send the device_token to my firebase database.
});
pushObject.on('notification').subscribe(()=>{
alert("new Message");
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error)); }

Ionic push notification is not working if app is open

I am able to send push form apps.ionic.io and from postman when app is on background or screen is locked but not able to send notification when app is open.
My code is as
in run
var io = Ionic.io();
var push = new Ionic.Push({
"onNotification": function(notification) {
alert('Received push notification!');
},
"pluginConfig": {
"android": {
"icon": "ic_stat_icon"
}
},
"debug": true
});
push.register(function(token) {
console.log("registered");
console.log("Device token:",token.token);
});
and in controller
$ionicPush.register( {
canShowconsole.log: true, //Can pushes show an console.log on your screen?
canShowAlert: true, //Can pushes show an alert on your screen?
canSetBadge: true, //Can pushes update app icon badges?
canPlaySound: true, //Can notifications play a sound?
canRunActionsOnWake: true, //Can run actions outside the app,
onNotification: function(notification) {
console.log(notification);
// Handle new push notifications here
alert('Received push notification!');
return true;
}
}).then(function(t) {
return $ionicPush.saveToken(t);
}).then(function(t) {
console.log('Token saved:', t.token);
});
Try this thing in your app.js or controller file.
$rootScope.$on('cloud:push:notification', function(event, data) {
var msg = data.message;
console.log(msg);
// alert(msg.title + ': ' + msg.text);
var alertPopup = $ionicPopup.alert({
title: msg.title,
template: msg.text.toString()
});
});

Unable to push GCM notification using node-gcm

I need to push GCM from node, I am using node-gcm, but I am getting ENOTFOUND error
var gcm = require('node-gcm');
var gcmMessage = new gcm.Message({
collapseKey: 'notify',
delayWhileIdle: false,
timeToLive: 3600,
data: {
title: "Hai",
message: "Message",
}
});
var gcmIds = ['<myid>'];
var sender = new gcm.Sender('<mysender id>',{'proxy':'<proxyip>:8080'});
sender.send(gcmMessage, gcmIds, 4, function(err, ret){
console.log('>>> sendGcm: ', err, ret);
});
I am getting error
sendGcm: { [Error: tunneling socket could not be established, cause=getaddr
info ENOTFOUND] code: 'ECONNRESET' } null

Categories

Resources