I am using fcm gem for sending push notifications in my rails application. I can able to retrieve the firebase registration id successfully.
But sending firebase notification results in Type Error ["registration_id"] is not a symbol or a string, the request is not hitting the firebase. My controller code is as follows,
class FirebaseNotificationsController < ApplicationController
require 'fcm'
def send_notification
#fcm = ENV['FCM_SERVER']
notification =
{
"notification":
{
"title": "test title",
"message": "test body"
}
}
response = #fcm.send(['firebase_registration_id'], notification)
end
end
I have even tried JSON parse, but still could not able to solve the error. What could be the solution and also my rails version is 5.2.3 and ruby version is 2.6.3.
Related
I am trying to fire a notification using gem 'apnotic'.
connection = Apnotic::Connection.development(cert_path: "#{Rails.root}/lib/mondofiPush.pem", cert_pass: "")
token = "5e67102820556ee177ffbe0a64ee3a391b89ac61ff2602c97587e838ef93cca"
notification = Apnotic::Notification.new(token)
notification.alert = "Notification from Apnotic!"
response = connection.push(notification)
But in response I get:
{"reason"=>"BadDeviceToken"} we checked that token if fine.
Can anybody please guide me to solve this?
I am using two firebase database projects. I have to send notifications from one database(Project-A) to another database(Project-B). I am sending notifications through API calls in cloud functions( in Project-A) and have to receive in IOS device ( from Project-B). I am sending the device token which is generated by using Project-A's SenderId through API call and saving that token in Project-A's database. In Project-A, using cloud function I am sending a notification to the IOS device in Project-B. In Project-A's firebase console it showing the notification is successfully sent, but in the receiver side, I am not getting any notification.
Payload:
let payload = {
notification: {
title: "Notification Alert",
body: "Notification Body",
sound: "default",
},
'data':{// these is the data it calls in the messagereceived method
'SenderID':senderauthid,
'ReceiverId':receiverauthid,
'SenderPhoneNumber':String(senderphonenumber),
'SenderName':Sendertotalname,
'flag':"3",
}
};//payload
return admin.messaging().sendToDevice(RegisterationTokens, payload).then((response)=> {
console.info("Successfully sent notification")
return res.status(200).json({Token : "Successfully sent notification"});
}).catch(function(error) {
console.warn("Error sending notification " , error)
});//end of notification
On successful notification message sent, I am receiving an error message stating that
errorInfo:
{ code: 'messaging/invalid-apns-credentials',
message: 'A message targeted to an iOS device could not be sent because the required APNs SSL certificate was not uploaded or has expired. Check the validity of your development and production certificates.' },
codePrefix: 'messaging' }
I have already added "APNs Auth Keys" in "APNs Authentication Key" section in firebase console. Do I have to make any changes?
I am new to Rails. I am trying to send push notification using rpush gem.
I am following these steps:
n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("android_app")
n.registration_ids = ["#{d.device_token}"]
n.data = { title: "#{self.title}"}
n.save!
The notification gets saved but is not getting delivered, and I get the following error description:
"Unable to deliver notification 10, received error (Failed to deliver to all recipients. Errors: NotRegistered.)"
Not able to understand the issue here. Please help me.
Thanks!
When you find above mentioned message in your log, means, as said #abielita and rpush wiki documentation, the identification_id GCM is using to send notification, is no more valid.
Device with (d.device_token) as registration token, is no more registered on GCM (Firebase) Service.
This message come for a specified reason: adapt your code to this condition.
Maybe is better destroy your "d" record !?
Becomes helpful Rpush.reflect in config/initializers/rpush.rb
Uncomment event notification on.gcm_invalid_registration_id and add your code inside block
e.g.
on.gcm_invalid_registration_id do |app, error, registration_id|
Device.where(registration_id: registration_id).take.destroy
end
and encloses your sending notification code inside a conditional check
unless(d.nil?)
n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("android_app")
n.registration_ids = ["#{d.device_token}"]
n.data = { title: "#{self.title}"}
n.save!
end
I hope it help
I've implemented Amazon SNS for push notifications.
I'm sending json in the following structure:
{
"aps":{
"alert":{
"loc-args":["ARGS"],
"loc-key":"KEY",
}
"sound":"default"
}
}
But on iOS client I'm receiving it as escaped string and everything from my message is inside "alert" key
[AnyHashable("aps"): {
alert = "{\"aps\":{\"alert\":{\"loc-args\":[\"ARGS\"],\"loc-key\":\"KEY\"},\"sound\":\"default\"}";
}]
I've also tried different formats from here http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html but every-time I'm receiving the same escaped string.
Does anyone had the same problem?
So the problem was in publishRequest.setMessageStructure("json"); flag.
I've missed it.
Also notification should be sent in the following format:
{"APNS_SANDBOX":"{\"aps\":{\"alert\":{\"loc-args\":[\"ARGS\"],\"loc-key\":\"KEY\"},\"sound\":\"default\"}}
I am sending push notifications from php job application to iphone. I am sending push notifications regarding new jobs. Is this possible that when user click on the view of push notification pop up , then user redirect to the particular job in the device.
I mean I wanted to know can I send any custom data with push notification like access_token, something else....so that Iphone end Can retrieve and show the particular job ?
Regardless of the language and library you use, the push notification payload is a JSON payload:
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
}
}
We want add custom information with the push notification. Like below
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
},
"access_token": 1
}
Thanks.
Finally, find the solution, How to add custom information with the push notification... We can add the custom information using the setPayload(). The apnspush() class file like this
<?php namespace Notificato\apnspush;
// This imports the Composer autoloader
require ROOT . "/vendor/autoload.php";
use Wrep\Notificato\Notificato;
class apnspush
{
/**
* This example sends one pushnotification with an alert to Apples production push servers
*/
public function sendOnePushNotification($deviceToken,$message,$accessToken)
{
$passphrase = 'asded333';
// First we get a Notificato instance and tell it what certificate to use as default certificate
$notificato = new Notificato("CBDEV.pem",$passphrase);
// Now we get a fresh messagebuilder from Notificato
// This message will be send to device with pushtoken 'fffff...'
// it will automaticly be associated with the default certificate
// and we will set the red badge on the App icon to 1
/* Make the JSON with access_token */
$pays = ["access_token"=>$accessToken];
$message = $notificato->messageBuilder()
->setDeviceToken($deviceToken)
->setAlert($message)
->setBadge($badge)
->setPayload($pays)
->setSound()
->build();
// The message is ready, let's send it!
// Be aware that this method is blocking and on failure Notificato will retry if necessary
$messageEnvelope = $notificato->send($message);
// The returned envelope contains usefull information about how many retries where needed and if sending succeeded
echo $messageEnvelope->getFinalStatusDescription();
}
}
?>