iOS Push Notification Does Not Work When Using Crontab Scheduler

iOS push notification does not work when using crontab scheduler

This problem was fixed. The real issue was in the PHP script I used.

Earlier in the stream_context_set_option I I did not include the full path to the ck.pem file . After giving the full path there was no error. Below is the code I am using right now.

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/Users/Development/Dev/ck.pem');

Some others who have had this problem and their discussions are

Apple Forum Question 1

Apple Forum Question 2

Ios push notification with PHP server Script using schedule task

After research a lot on it at last i found where is the loop hole..

you have to change just one line to work this method with the cron job..:-)

change this line

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'sample.pem');

to

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'your FULL path of pem file');

for my case i used

stream_context_set_option($streamContext, 'ssl', 'local_cert', 'C:\Websites\games\project_name\sample.pem');

and that's all... hope your problem will be solved now..

CloudKit for sending push notifications through cron jobs?

It seems incredible you wouldn't use or consider Parse.com for something like this ...

(If not Parse, some other bAAs with a similar feature set.)

NOTE - Parse is now at back4app.com.

1) Parse is the easiest possible way to do push with an iOS app

2) the whole idea of Parse is that you have cloud code, and it's incredibly simple,

https://parse.com/docs/cloud_code_guide

you can have cloud code routines, and, you can have "cron" routines that go off regularly. it's why everyone's using Parse!

Note that it is super-easy to call "cloud functions" from iOS, with Parse.

Here's an example,

-(void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
int thisRow = indexPath.row;
PFUser *delFriend = [self.theFriends objectAtIndex:thisRow];

NSLog(@"you wish to delete .. %@", [delFriend fullName] );

// note, this cloud call is happily is set and forget
// there's no return either way. life's like that sometimes

[PFCloud callFunctionInBackground:@"clientRequestFriendRemove"
withParameters:@{
@"removeThisFriendId":delFriend.objectId
}
block:^(NSString *serverResult, NSError *error)
{
if (!error)
{
NSLog(@"ok, Return (string) %@", serverResult);
}
}];

[self back]; // that simple
}

Notice I'm calling a cloud function "clientRequestFriendRemove". So that's just a piece of cloud code I wrote and which is on our Parse account, in fact here it is

Parse.Cloud.define("clientRequestHandleInvite", function(request, response)
{
// called from the client, to accept an invite from invitorPerson

var thisUserObj = request.user;
var invitorPersonId = request.params.invitorPersonId;
var theMode = request.params.theMode;

// theMode is likely "accept" or "ignore"

console.log( "clientRequestAcceptInvite called.... invitorPersonId " + invitorPersonId + " By user: " + thisUserObj.id );
console.log( "clientRequestAcceptInvite called.... theMode is " + theMode );

if ( invitorPersonId == undefined || invitorPersonId == "" )
{
response.error("Problem in clientRequestAcceptInvite, 'invitorPersonId' missing or blank?");
return;
}

var query = new Parse.Query(Parse.User);
query.get(
invitorPersonId,
{
success: function(theInvitorPersonObject)
{
console.log("clientRequestFriendRemove ... internal I got the userObj ...('no response' mode)");

if ( theMode == "accept" )
{
createOneNewHaf( thisUserObj, theInvitorPersonObject );
createOneNewHaf( theInvitorPersonObject, thisUserObj );
}

// in both cases "accept" or "ignore", delete the invite in question:
// and on top of that you have to do it both ways

deleteFromInvites( theInvitorPersonObject, thisUserObj );
deleteFromInvites( thisUserObj, theInvitorPersonObject );

// (those further functions exist in the cloud code)

// for now we'll just go with the trick of LETTING THOSE RUN
// so DO NOT this ........... response.success( "removal attempt underway" );
// it's a huge problem with Parse that (so far, 2014) is poorly handled:
// READ THIS:
// parse.com/questions/can-i-use-a-cloud-code-function-within-another-cloud-code-function
},
error: function(object,error)
{
console.log("clientRequestAcceptInvite ... internal unusual failure: " + error.code + " " + error.message);
response.error("Problem, internal problem?");
return;
}
}
);

}
);

(Fuller examples ... https://stackoverflow.com/a/24010828/294884 )

3) it's trivial to make Push happen from the cloud code in Parse, again this is why "everyone uses it"

For example, here's a Parse cloud code fragment that relates to Push ...

function runAPush( ownerQueryForPush, description )
// literally run a push, given an ownerQuery
// (could be 1 to millions of devices pushed to)
{
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.matchesQuery('owner', ownerQueryForPush);
Parse.Push.send
(
{
where: pushQuery,
data:
{
swmsg: "reload",
alert: description,
badge: "Increment",
title: "YourClient"
}
},
{
success: function()
{ console.log("did send push w txt message, to all..."); },
error: function(error)
{ console.log("problem! sending the push"); }
}
);
}

4) it's unbelievably easy to do everything relating to your food database, on a nosql environment. nothing could be easier than the Parse approach

5) you get the whole back end for free (for adding foods, whatever) - normally months of work

6) finally i guess Parse is quite free (until you have so many users you'd be making a fortune anyway)

So, I can't imagine doing what you say any other way - it would be a nightmare otherwise. Hope it helps

Problem with notification scheduling in WEB Applications

I think you need a place where scheduled and finished notifications will be persisted, independently on what you are using, cron or at.

If I had such a task, I would stay with a solution like this: run special script, "scheduler.php" each 1 (or more, e.g. 5) mins by cron, which will check some log file(or remote database in case of several machines) and look if there are any new lines. If new line present and it contains timestamp in the past and status "sceduled", than script will lock it and run your "sender.php". After that it will mark the line as "done". Each line in a storage should contain a timestamp to run and one of three statuses "scheduled", "running" and "done".

With such approach you could plan new notifications by adding a line with needed time and status "scheduled" to the storage. Note, that there can be a little delay between scheduled time and actual notification depending on the cron interval, but I suppose it is not critical.

This will allow you to run any number of crons on different machines and guarantee that each job will be done once.

Important: if you will adopt this scheme, be sure that your scheduler.php reads and updates a storage in a single atomic operation, to prevent race conditions between several crons. File locks, or "select for update" will do.

What is the right way to implement scheduled push notifications?

Cron job is a scheduled task that you can run on any time interval. It is OK to run it every 1 minute depending on your requirement.

You can increase the efficiency of your cron in this kind of a situation.

You run the file -> Check to see you have any notifications to send -> If you have continue your logic.

If you don't have anything you can just exit;

Meaning, it will give some load to the server only when you have some thing to do (which we should be doing). Running a cron every minute will not give you huge server load (again depends on how well you structure your code / db queries to optimize usage)

Hope this gives you a bit of head start!

How can I create a push notification that goes off after x number of seconds? (iPhone/iPad)

Have you considered using local notifications instead? You can schedule it to go off at a certain time without having to make the server round-trip

Continue running code in background for Sending Notification to a Single User

This is a bit of a tricky problem unfortunately.

  1. Apple won't allow you to create long-running operations in the background. As you mentioned, you can add code in the background with location updates or audio, but neither of those are very good solutions since (1) Apple will likely reject you from the app store and (2) the user will probably close the app if they notice a big locations banner at the top of their phone (or a weird background audio signal), and closing the app will kill your operation. For any truly long-running tasks it's probably best to set up a server and run a cron job or equivalent long-running operation. It's the only way you can know that the task will continue running.

  2. Sorry, I was just answering one at a time, but as I mentioned above, yes, they'll probably reject your code for that. As a rule of thumb: if you think the Apple-created code you're using wasn't meant for what you're using it for, Apple will probably reject your app.

  3. It sounds like you should use a remote notification. If you do use a remote notification, you'll probably want some sort of backend anyway. You could use a local notification to notify a user after a few months, but you would need to know the exact time to send it up front, which it doesn't sound like you would.

In summary: Try to build a simple BE. Maybe use Firebase Functions or something, and also build an APNS system to send pushes. In the app, tell the server to start processing the information and send the push back when it's done. The situation sounds like a perfect use case for server-side logic.

Hope that helps!



Related Topics



Leave a reply



Submit