Search code examples
javascriptswiftparse-platform

Push Notifications with PFCloud Code


I am currently using back4app as my parse host.

I have push notifications correctly sending to the required devices when using Parse.Cloud.afterSave. So as I can rule out server setup issues.

I want to also detect when a row is deleted and I believe I can do this by using the Parse.Cloud.beforeDelete function.

My Parse.Cloud.beforeDelete function looks like the following

Parse.Cloud.beforeDelete("CallSession", function(request, response) {
  query = new Parse.Query("CallSession");
  query.equalTo("callerID", request.object.get('callerID'));

    var recieverID = request.object.get('receiverID');
    var sessionID = request.object.get('sessionID');
    var subscriberToken = request.object.get('subscriberToken');
    var publisherToken = request.object.get('publisherToken');
    var callerTitle = request.object.get('callerTitle');
    var callerImageURL = request.object.get('callerImageURL');
    var isVideoCall = request.object.get('isVideo');
    var pushID = request.object.get('pushID');
    var callerID = request.object.get('callerID');

    var callerObjectID = request.object.get('callerObjectId');
    var publicFigureObjectID = request.object.get('publicFigureObjectId');


    Parse.Push.send({
        where: query, // Set our Installation query
        data: {
            "content-available" : 1,
            "sessionID" : sessionID,
            "subscriberToken" : subscriberToken,
            "publisherToken" : publisherToken,
            "callerTitle" : callerTitle,
            "callerID" : callerID,
            "callerObjectId" : callerObjectID,
            "publicFigureObjectId" : publicFigureObjectID,
            "callerImageURL" : callerImageURL,
            "isVideo" : isVideoCall,
            "pushID" : "endCall"

        }
    },{success: function() {
        return response.success('endcall Sent OK');
    },
        error: function(error) {

            console.log('Push AfterSave Error = ' + error);

            return response.success(error);
        },
        useMasterKey: true

    });
  });

The server logs are showing that the beforeDelete is being triggered however the push is not sending to the device.

Would anybody know what I'm doing wrong with the above cloud function?

thanks in advance

Thomas Richardson


Solution

  • For what I could check the amount of data that you're inserting within the Parse.Push.send() exceed the preset from Parse Server basic settings. As you can check in their Documentation:

    If you want to send more than just a message, you can set other fields in the data dictionary. There are some reserved fields that have a special meaning.

    • alert: the notification’s message.
    • badge: (iOS only) the value indicated in the top right corner of the app icon. This can be set to a value or to Increment in order to increment the current value by 1.
    • sound: (iOS only) the name of a sound file in the application bundle.
    • content-available: (iOS only) If you are a writing an app using the Remote Notification Background Mode introduced in iOS7 (a.k.a. “Background Push”), set this value to 1 to trigger a background download.
    • category: (iOS only) the identifier of the UNNotification​Category for this push notification.
    • uri: (Android only) an optional field that contains a URI. When the notification is opened, an Activity associated with opening the URI is launched.
    • title: (Android only) the value displayed in the Android system tray notification.

    This can be improved and specific needs can be added however, as you may check the Documentation, you'll need to configure your app to have those settings and handle what the Cloud Code can't.

    Another thing that you might need to consider is checking if the results of your query matches what you desire to use in the subsequent lines of code by debugging it (adding some logs showing the content of your query response should be enough).