Sending silent push notifications using Parse.com

2.4k views Asked by At

I was wondering if there is a good way to send silent push notifications to users using the parse.com services.

By "silent", i mean no actual notification if the user is in the app (I would send normal one if the user was out of the app), no "alert" message, no nothing. Just a discrete function call.

I need this to perform some code while the user is in the app.

I've read in the doc that I can use cloudcode but

  1. Is it best?
  2. How can i do it? there is no other explanation about it.
  3. Is there another way that is more efficient/mobile friendly to call a function remotely without the user noticing.

Should I use obj-C code? cloud code? Can you provide a small example ? (I really just need to call a "refresh" function in my code silently, nothing fancy)

Thanks a lot :)

3

There are 3 answers

0
Smiless On BEST ANSWER

I do this on for me and it works .

First : In your project capabilities go to "background" and check "remote notification"

Second : In your appdelegate be sure to have this method which handle the background (silent) push.

-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

//i handle the silent push here with a test on the userinfo's param.
// if content-available = 1 do some stuff
// else 
    // [PFPush handlePush:userInfo];

}

and finally : When you set the data in your push you have to add "content-available" = 1 and remove the sound

 NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          temp, @"alert",
                          @"Increment", @"badge",
                          @"", @"sound",
                          @1, @"content-available",
                          nil];

or

NSDictionary *data =@{
      @"badge": @"Increment",
      @"alert": temp,
      @"sound": @"",
      @"content-available" :@1
      };
0
Chan On

You can use cloud code functionality for such type of scenarios. I'm not that much aware of parse cloud functionality. Here's the sample cloud code of what you expect.

This is how you define a cloud function

Parse.Cloud.define("SomeFunction", function(request, response) {
//Write queries as you need
});

Then call this cloud function as follows
[PFCloud callFunctionInBackground:@"SomeFunction" withParameters:parameters block:^(id object, NSError *error) {
//Add this function in some method & call the method wherever you needed, suppose if you need to update your app which has SwipingSideMenu like functionalities, for each time you click on the menu or side button call this cloud function. Thats it.
}];
0
Siddhant Jain On

Also you can refer to https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

The aps dictionary can also contain the content-available property. The content-available property with a value of 1 lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.