0

I am trying to send a notification when a number is written to _random. I am able to get the device token, and the cloud function works perfectly. However, I do not receive the notification on the simulator. I am using the notification token that is pushed to Firebase to test with. If anybody can help, it will be highly appreciated.

https://i.stack.imgur.com/OB94c.png

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

//Initial function call:
exports.makeRandomFigures = functions.https.onRequest((req, res) => {
    //create database ref
    var rootRef = admin.database().ref();
    var doc_count_temp = 0;
    var keys = [];
    var random_num = 0;

    //get document count
    rootRef.once('value', (snapshot) => {
        doc_count_temp = snapshot.numChildren();
        //real number of member. if delete _timeStamp then minus 2 not 3!
        var doc_count = doc_count_temp - 3;

        //get num array previous generated
        var xRef = rootRef.child("_usedFigures");

        xRef.once('value', function(snap) {
            snap.forEach(function(item) {
                var itemVal = item.val();
                keys.push(itemVal);
            });
            //get non-duplicated random number
            var is_equal = true;
            while (is_equal) {
                random_num = Math.floor((Math.random() * doc_count) + 1);
                is_equal = keys.includes(random_num);
            }

            //insert new random vaule to _usedFigures collection
            rootRef.child('_usedFigures').push(random_num);
            rootRef.child('_random').set(random_num);
        });
    });

    //send back response 
    res.redirect(200);
});

exports.sendFigureNotification = functions.database.ref('_random').onWrite(event => {

    const payload = {
        notification: {
            title: 'Title',
            body: `Test`, //use _random to get figure at index key
            badge: '1',
            sound: 'default'
        }
    };
    
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24, //24 hours
        content_available: true
    };
   const token = "cge0F9rUTLo:APA91bGNF3xXI-5uxrdj8BYqRPkxUPA5x9IQALtm3VEFJAdV2WQrQufNkzIclT5B671mBcvR6IDMbgSKyL7iG2jAuxRM3qR3MXhkNp1_utlXhCpE2VZqTw6Yw3d4iMMvHl1B-Cvik6NY";
    console.log('Sending notifications');
    return admin.messaging().sendToDevice(token, payload, options);

 
});
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • You're not making use of the promises from any of the APIs you're calling in `makeRandomFigures`. It turns out you're sending a response (and terminating the function) before any of the async work completes. That will almost certainly cause problems. – Doug Stevenson Jan 30 '18 at 03:23

1 Answers1

1

You can't get push notifications on the simulator.

To try some alternative ways check this link :

How can I test Apple Push Notification Service without an iPhone?

Ashish Sharma
  • 663
  • 5
  • 15
  • Successfully sent message: { results: [ { messageId: '0:1517287541355690%4448dfb34448dfb3' } ], canonicalRegistrationTokenCount: 0, failureCount: 0, successCount: 1, multicastId: 7812858106942759000 } – Eric Dowdell II Jan 30 '18 at 04:46
  • @EricDowdellII That's a firebase response, it has nothing to do with a simulator or a real device. Make sure you are properly using proper functions to fetch and present notifications on the device. – Ashish Sharma Jan 30 '18 at 07:25
  • So something odd is going on.. The push notification works on my project partner's device but not mines. The code is exactly the same too – Eric Dowdell II Jan 30 '18 at 16:36
  • make sure you have allowed push notifications in your device.. try to remove and reinstall the app.. in case you are using USB installation, it should be fine. If you are using over-air installation make sure to add device udid to provisioning profile.. or finally, Maybe you can try this link. https://stackoverflow.com/questions/48217757/not-receiving-push-notifications-from-firebase/48220042#48220042 – Ashish Sharma Jan 30 '18 at 16:52
  • Hey I figure it out/ I had to change the FirebaseAppDelegateProxyEnabled to YES... It will not do it unless it is changed – Eric Dowdell II Jan 30 '18 at 17:50
  • @EricDowdellII if the above link helped in doing this please upvote that answer. That's my answer only. – Ashish Sharma Jan 31 '18 at 03:46