How to Handle push notification when sent to multiple device but some them are failed - firebase-cloud-messaging

I want to send fcm push notifications to android and ios app. I am using this code to send
let message = {
registration_ids: firebaseId, // this is the array of tokens
collapse_key: 'something',
data: {
type: data.type,
title: data.title,
body : data.body,
notificationId: something
},
};
fcm.send(message, (err, response) => {
if (err) {
console.log(err);
resolve(false);
} else {
console.log("Notification Android Sent Successfully");
console.log(response);
}
});
Now what I want if some notifications are failed then I want to send them SMS. but I got a response like this form the fcm server in case of success or failure.
Notification Android Sent Successfully
{"multicast_id":7535512435227354255,"success":2,"failure":1,"canonical_ids":0,"results":[{"message_id":"0:1610622370449056%d0bd483c86f03759"},{"message_id":"0:1610622370449058%d0bd483c86f03759"},{"error":"InvalidRegistration"}]}
now how will I know which device did not get the notification as we can see there 1 failure from 3 device
so i can send SMS to that device

The results in the response you get contains the result for each token, in the same order in which you specified them,
So in your example, the message was successfully sent to the first two tokens, while the third token was unknown. You'll want to remove that last token from your database to prevent trying to send messages to it in the future.

Related

Supabase Twilio Phone Auth

I am building a react native app using Supabase with phone auth on Android emulator.
I have set my twilio and Supabase up, but when I call supabase.auth.signUp(), it does not send a OTP to my phone number.
This is my code:
async function signInWithPhoneNumber() {
let { data, error } = await supabase.auth.signUp(
{ phone: '+61 xxx xxx xxx',
password: 'some-password' }) setConfirm(data);
console.log(data)
if (error) {
console.log('error')
return } }
And I just have a button that calls this function. The 'xxx xxx xxx' is a placeholder for my actual number. I tried using the provided Twilio phone number as well but to no success. My twilio account is working well, as i was able to send a message from Twilio to my real phone. And I correctly put in the phone auth details for Supabase.
My log for the 'data' gives me: {"session": null, "user": null}
And if i was to log the actual error, it gives me: [AuthRetryableFetchError: Network request failed]
Anyone know the issue?
Thanks!
I don't think you can use phone authentication with password: only OTP. Have you tried the following:
await supabase.auth.signInWithOtp({
phone: '+61xxxxxxxxx',
})
await supabase.auth.verifyOtp({
phone: '+61xxxxxxxxx',
token: 'xxxxxx',
type: 'sms'
})
see also:
https://supabase.com/docs/reference/javascript/auth-signinwithotp
https://supabase.com/docs/reference/javascript/auth-verifyotp

Can't receive SMS message with Twilio trial account

I have a small project (Express) using a Twilio trial account to send an SMS message to my phone number. But when I use my own message to replace the example string, I can't receive the SMS anymore.
This is what I tried:
Verified my own number, so the Twilio phone number can send SMS messages to my own phone number.
Verified the token and sid are the live credentials, not the testing one.
Check the SMS Log, and the status of all messages are sent. But again, I don't receive anything.
Used postman to verify the route if it is working properly. And yes, it did.
This is my code for the route to call the function to send the SMS
router.post("/contacts/sms", authorization, async (req, res) => {
try {
const { phone, message } = req.body;
**sendSms(phone, message);**
res.status(201).send({
message:
"Account created successfully, kindly check your phone to activate your account!",
});
} catch (error) {
res.status(500).send("Server error");
}
});
This is the SMS function
require("dotenv").config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const sendSms = (phone, message) => {
const client = require("twilio")(accountSid, authToken);
client.messages
.create({
body: message,
from: process.env.TWILIO_PHONE_NUMBER,
to: phone,
})
.then((message) => console.log(message.sid));
};
module.exports = sendSms;
I have answered a similar question, you might wanna have a look at that
https://stackoverflow.com/a/71912348/17503984
The problem might be since your Twilio phone number and the number you are sending are from different nationalities, communication problems arise due to the international SMS service

Figuring out which FCM tokens are expired from a firebase response with several registration ids

Firebase rest endpoint for push notifications lets you send a message to several people at once by passing the tokens through "registration_ids" array instead of "to". When the response comes back it looks like this:
{
"multicast_id":000000000000000000,
"success":1,
"failure":4,
"canonical_ids":0,
"results":[
{
"error":"NotRegistered"
},
{
"error":"NotRegistered"
},
{
"error":"NotRegistered"
},
{
"error":"NotRegistered"
},
{
"message_id":"0:0000000000000000%a0a0a0aaa0a0a0aa"
}
]
}
How are you supposed to correlate the NotRegistered errors to a particular token it was sent to?
The results are in the same order as you specified them in the request. So only your last token was valid.

Can't get GCM push messages being sent properly

So my GCM push message works if I use this test link
http://www.androidbegin.com/tutorial/gcm.html
Here's the response
{ "multicast_id":7724943165862866717,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[{"message_id":"0:1418649384921891% 7fd2b314f9fd7ecd"}]}
However if I push using my own service using node push service using the toothlessgear/node-gcm lib
https://github.com/ToothlessGear/node-gcm I get a success message on the server but no msg makes it to the client
{ multicast_id: 5130374164465991000,
success: 1,
failure: 0,
canonical_ids: 0,
results: [ { message_id: '0:1418649238305331%7fd2b3145bca2e79' } ] }
I also tried the same message using pushwoosh and push woosh doesn't work either. How come I'm getting a success message on the server, but no push is received on the client on the latter two services. Is there some sort of ip configuration that I need to do, or some sort of certificate? I've used the same google api server key which is open to all ips on all 3 of these services.
Why does the response show success on the latter but no msg gets received on the client?
Node service server side code
var gcm = require('node-gcm');
// create a message with default values
var message = new gcm.Message();
// or with object values
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: 'message1',
key2: 'message2'
}
});
var sender = new gcm.Sender('insert Google Server API Key here');
var registrationIds = ['regId1'];
/**
* Params: message-literal, registrationIds-array, No. of retries, callback-function
**/
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
So the pushes were correctly being sent, my issue was with the cordova plugin on the client which requires that the android payload for "message" or "title" be set. The sample php just coincidentally was setting the message property and that's why it worked.
Updating the code to add the following to the data
data: {message:'test'}
works correctly

Fb.ui apprequest returning null response

After sending an Facebook app request in FB.ui using Javascript SDK, the response returned in the callback function is 'null', if the user sends the request or not. I have tested with multiple Facebook accounts and receive the app requests, however.
, sendRequest: function() {
parent.FB.ui({method: 'apprequests',
to: this.game.attributes.opponentFbId,
title: 'My App',
message: 'I sent you a challenge on My App',
}, function (response) {
console.log(response)
});
According to http://fbdevwiki.com/wiki/FB.ui, method 'apprequests' should return an array of request_ids (via response.request_ids), or response.to .However 'response' returns null when I try to log it in console
I have tried something similar with method 'feed' in FB.ui and can read the response (and response.post_id), however not with 'apprequests'.