3

This is what my log looks like when my push notification gets called on

I am currently working on creating push notification set up for a user to user setting for the iPhone. I am currently using Firebase, so naturally I turned to Firebase Cloud Messaging to get this done. This is my setup in the functions that I am deploying to my Firebase. Is there something that I am doing wrong in here that would result in the notification not being sent to the device? I appreciate any help, and if there is any more needed information I would be happy to supply it.

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// Listens for new messages added to messages/:pushId
exports.pushNotification =     functions.database.ref('/messages/{pushId}').onWrite( event => {

console.log('Push notification event triggered');

//  Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
console.log(valueObject)

if(valueObject.photoUrl != null) {
  valueObject.photoUrl= "Sent you a photo!";
}

 // Create a notification
const payload = {
    notification: {
        title:valueObject.toId, 
        body: valueObject.text || valueObject.photoUrl,
        sound: "default"
    },
};

//Create an options object that contains the time to live for the notification and the priority
const options = {
    priority: "high",
    timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic("pushNotifications", payload, options);
 if(!data.changed()){

});

exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite( event => {
const data = event.data;
console.log('Push notification event triggered');
return;
}



});
  • Have you upload p12 on firebase cloud messaging section ? – Jignesh Mayani May 06 '17 at 06:05
  • Hey thanks for the reply! i'm not quite sure what a p12 is? @JigneshMayani – oneQuestionAsker May 06 '17 at 06:13
  • Are you getting an error in the response? – AL. May 06 '17 at 10:03
  • First APNS has 2 certificates 1 for development (if you passing ipa, compile directly on device) or for distribution (if you use testflight or appStore) in fire base you must specify development or production and choose certificate. App signed by development gets a token for Certificate Development (doesn´t work for distribution) and vice versa. – Jose Pose S May 06 '17 at 10:37
  • I updated the question with a picture of what the logs look like @AL. – oneQuestionAsker May 06 '17 at 14:44
  • I currently only have the development APNs certificate attached with my Firebase, I didn't attach anything for Production APNs certificate @JosePoseS – oneQuestionAsker May 06 '17 at 14:47
  • I'm also facing the same issue. Android devices get the push notification but not iOS devices. Any resolution for this? – Hashan Seneviratne Aug 17 '17 at 23:15

2 Answers2

0

I noticed that you are exposing the same function twice. That is also an issue. Also I suggest you to promisify the admin.messaging, so that you can handle and check for errors.

let topic = "pushNotifications";
admin.messaging().sendToTopic(topic, payload, options)
        .then(function(response) {

            console.log("Successfully sent message:", response);
            console.log("Topic: " + topic);
            res.status(200).send("success");
        })
        .catch(function(error) {
            console.log("Error sending message:", error);
            res.status(500).send("failure");
        });
Hashan Seneviratne
  • 1,157
  • 13
  • 24
-1

Send this jsone on your post parameter in registration_ids field you have to post array of your All device token that you want to send push notification This is post request method body

{ "registration_ids" : [Send Array of Device Token], 
  "data" :
    {
    "image_url" : "send your image here"
    "message" : "Send your message here" },
 "notification" : 
   {
    "title" : "APP Name",
    "sound" : "default",
    "priority" : "high"
   }
}

Here is post URL

https://fcm.googleapis.com/fcm/send

and send key="Your Authorization key" in request HttpHeader field

Take reference for basic setup form here for cloud messaging

https://firebase.google.com/docs/cloud-messaging/ios/client

Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
  • In OP's case, he's clearly using the Firebase Admin module to make the request (via admin.messaging()). I wouldn't make an HTTP request as you're suggesting here. The link you referenced only references Swift and Objective C and OP's question is with regards to the JS libraries. For those reasons alone, I'm downvoting your answer. – AlxVallejo Jul 05 '17 at 15:16