Parse iOS - Send push notification at specific time - ios

A part of my app currently lets a user schedule appointments using a UIDatePicker. They press a button and my backend does some work with the date to store the appointment data, then I send a push using the following code:
NSString *objectID = [NSString stringWithFormat:#"user_%lu", (unsigned long)self.salespersonObject.userID];
PFPush *push = [PFPush new];
[push setChannel:[NSString stringWithFormat:#"%#",objectID]];
NSString *message1 = [NSString stringWithFormat:#"%# sent you an appointment request. Visit your profile in the app to confirm", self.currentUser.fullName];
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
message1, #"alert",
#"Increment", #"badge",#"default",#"sound",
nil];
[push setData: data];
[push sendPushInBackground];
What I'm wondering ... is there any way using the Parse iOS SDK to set a specific time for a push to be delivered? I'd like to also in this method add a push to be sent 1 hour prior to the UIDatePicker date that says something like "Reminder your appointment with x is in 1 hour"
Thanks in advance!

From Parse Doc :
Sending scheduled push notifications is not currently supported by the
iOS or OS X SDKs. Take a look at the REST API, JavaScript SDK or the
Parse.com push console.
The best thing to do IMO is to use Cloud Code, so you can make a trigger (afterSave, afterDelete) or a function that you call from your iOS App.
Since CloudCode use the Javascript SDK you can make something like that:
var tomorrowDate = new Date(...);
var query = new Parse.Query(Parse.Installation);
query.equalTo('user_id', 'user_123');
Parse.Push.send({
where: query,
data: {
alert: "You previously created a reminder for the game today"
},
push_time: tomorrowDate
}, {
success: function() {
// Push was successful
},
error: function(error) {
// Handle error
}
});
Parse Push notification scheduling Guide

You can use parse Background jobs to schedule the push notification.
Here is the link for documentation

There really isn't a whole lot you can do with the iPhone SDK and a closed app.
I would suggest that you assign the push to a channel, sending notifications is often done from the Parse.com push console, the REST API or from Cloud Code, you can also assign background jobs through this medium.
But if you want to stick to objective C as you don't mention any other languages; You can send Local Push Notifications natively but not through the PARSE SDK.

Related

How to block specific user push notification in parse iOS app?

I am working on a iOS chatting app by use parse as backend service. If user blocks another user, how to prevent the push notifications? Is that possible to filter this push notification in parse side?
Really appreciate in advance.
There are several different ways to block a push notification from being sent to a user that blocked the sender. It really depends on how you handle the blocking of the user.
You could, for instance, add a blockedUsernames array to each user. If you did this, you could block the push notification from being sent in the first place, by cross checking this blocked users array against the users it is being sent to.
// CHECK FOR BLOCKED USERS
PFUser *currentUser = [PFUser currentUser]; // user sending push
PFUser *sendPushToUser = //user receiving the push
// Get array of blocked users
NSMutableArray *blockedUsersArray = sendPushToUser[#"blockedUsers"];
BOOL blocked = false;
for (NSString *username in blockedUsersArray) {
if ([username isEqualToString:currentUser.username]) {
blocked = true;
}
}
// If the user isn't blocked, send push
if (blocked == false) {
[PFCloud callFunctionInBackground:#"sendPushToUser"
// more cloud code to handle the notification...
You could also block the push notification in the Parse Cloud Code, with some JS.

How to send push notification after a row is inserted in parse for iOS(Obj-C)

I have an Order table in parse. After a row is inserted in to the table, I want to send a push notification to the iOS device. How to write a cloud code for this specific task? I read parse documentation but I couldn't understand about channels, installation and other stuff related to push notifications. I have seen various blogs regarding this but I couldn't get a right answer for cloud code and how to call that code in iOS. Any help is appreciated. Thank you.
You don't say which devices you want to send the push notification to, so I can't provide a more specific example, but here is an afterSave method for a class called "Location". In this code, a query is defined that identifies all installations that are associated with the updated location and a 'silent push' is sent to these devices -
Parse.Cloud.afterSave("Location", function(request) {
query = new Parse.Query("Installation")
query.equalTo("location",request.object)
Parse.Push.send({
where: query,
data: {
"content-available" : "1",
event: "locationUpdate"
}
},{
success:function() {
},
error:function() {
console.error("Error in push "+error.code+" : "+error.message);
}
});
});

send message to loggded In user using parse

I have created a database on parse.com
I want to send push notification to logged in user
I have created "Installation" class.
in that class I have added two devices with device token.
and after logged in app I have added field "owner" with loginId.
After that I am trying to send push notification to logged in used from my device but my code not working
here is code of send button
- (IBAction)WRMethodBtnSendMsg:(id)sender {
// Send a notification to all devices subscribed to the "Giants" channel.
PFQuery *tmpQuery = [PFInstallation query];
[tmpQuery whereKey:#"owner" containsString:[self.sdPFObject objectId]];
PFPush *push = [[PFPush alloc] init];
[push setQuery:tmpQuery];
[push setMessage:self.wrTxtMsg.text];
[push sendPushInBackground];
}
but push notification doesn't send
Appreciate for help
I'd use channels. Have every user subscribe to a "user channel", something along the line of user_*user_id_here*. Then, when you want to send a push notification to the user with ID 123456, simply send it to the user_123456 channel.

Parse : How to send a push notification to everyone from iOS app?

is there a way to send a push notification to everyone directly from app, as in the console ?
if i try following code :
PFPush *push = [[PFPush alloc] init];
[push setMessage:#"Invio Push"];
[push sendPushInBackground];
looking at the consolle
it says that has been sent to 0 users,and it tried to send to channels i/o everyone.
If i send the message from console, i manage to send to everyone, without problem
Is there a way to achieve the same results from app ?
P.s.
naturally if I add
PFPush *push = [[PFPush alloc] init];
[push setChannel:#"Mychannel"];
[push setMessage:#"Invio Push"];
[push sendPushInBackground];
the push notification is sent correctly to "Mychannel" channel
Many thanks
Fabrizio
Yes you can also send push notifications directly from a mobile application. Remember that you need to have enabled this feature in the Parse app's settings tab by selecting "Yes" under the heading "Client push enabled?". There are several methods that can be called to send push notifications.
My Example:
// Create our Installation query
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:#"deviceType" equalTo:#"ios"];
// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
withMessage:#"TEST1234"];

Is there a way to send user-specific push notifications for iOS devices?

I would like to know if I can create a service to send customized user-specific push notifications for iOS.
Example
#"Hey %#, how you doin", firstName"
Is this possible?
Unless I completely misunderstood what you need, no answer has what you need.
Here's a relevant example from the APNS guide :
let’s consider an example. The provider specifies the following dictionary as the value of the alert property:
{ "aps" :
{
"alert" : {
"loc-key" : "GAME_PLAY_REQUEST_FORMAT",
"loc-args" : [ "Jenna", "Frank"]
}
}
}
When the device receives the notification, it uses "GAME_PLAY_REQUEST_FORMAT" as a key to look up the associated string value in the Localizable.strings file in the .lproj directory for the current language. Assuming the current localization has an Localizable.strings entry such as this:
"GAME_PLAY_REQUEST_FORMAT" = "%# and %# have invited you to play Monopoly";
the device displays an alert with the message “Jenna and Frank have invited you to play Monopoly”.
Of course. Check out the APNS programming guide, specifically the payload, which you can customize on your server before you send it to the user's device.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
Also note that if you know when the notification should appear (not at a dynamic time) then look into local notifications, which don't require a server backend.
You can use any service for Push Notification (as listed) or do it by yourself.
In your code, when you receive the Push Notification message:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
//here you should treat your received data
}
There are some very good solutions for iOS push notifications service that use APNS. No need to implement it on your own.
PushApps - free for 1M notifications per month, and unlimited notifications for 19.99 per month, and you can import your users via the API or by sending the a csv file - documentation
Urban Airship - free up to 1M notifications per month, afterwards you are charged per 1000 notifications
Parse - also free for the first 1M notifications
PushWoosh - free for 1M devices, premium plans are from 39 EURO
If you have to implement it on your own, have a look at easyAPNS if you want to host it yourself.
Another good site for info is Ray Wenderlich's site which hosts a 2 part tutorial:
Apple Push Notification Services Tutorial: Part 1/2
Apple Push Notification Services Tutorial: Part 2/2
Or this, Apple Push Notification module for Node.js.
Of course you can...
You need to create your own back-end php file for push notification service, you can send json data from your app to the php file and use $GET to achieve the data you want, including device token, message, badge number... please refer to this
in your viewController
NSString *finalUrl =[NSString stringWithFormat:#"%#/pushService.php?device_token=%#&passphrase=%#&msg=%#",baseURL,device_token,passphrase,msg];
NSURL *url =[NSURL URLWithString:finalUrl];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
NSError *error = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(#"result: %#", result);
}else
{
NSLog(#"connection error!");
}
pushService.php file
// Put your device token here (without spaces):
$deviceToken = $GET['device_token'];
// Put your private key's passphrase here
$passphrase = $GET['passphrase'];
// Put your alert message here:
$message = $GET['msg'];
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 5
);

Resources