Apple Push Notification with Sending Custom Data

Apple Push Notification with Sending Custom Data

Regardless of the language and library you use, the push notification payload is a JSON payload:

{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
}
}

The aps token is the Apple APN data. You can add custom data to your payload as well:

{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
},
"job_id": 1
}

When you receive the notification in the app, check for your param in the notification dictionary:

- (void)handleBackgroundNotification:(NSDictionary *)notification
{
NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
NSMutableString *alert = [NSMutableString stringWithString:@""];
if ([aps objectForKey:@"alert"])
{
[alert appendString:(NSString *)[aps objectForKey:@"alert"]];
}
if ([notification objectForKey:@"job_id"])
{
// do something with job id
int jobID = [[notification objectForKey:@"job_id"] intValue];
}
}

Keep in mind that the total size of the payload is 256 bytes, and that includes, of course, your custom parameters. So you may have to (at risk of reducing readability) call your custom param "ji" instead of "job_id" to squeeze bytes.

All of this is documented in the Local and Push Notification Programming Guide in the iOS documentation. Definitely would recommend a read because it's more complex than it initially sounds (at least, that's what I thought).

Node js apple push notification with sending custom data

I think you can set it in payload

note.payload = {'messageFrom': 'Caroline', 'ntfnDetails' : ntfnDetails };

Check this apple-push-notification-with-sending-custom-data

Can apple push notifications send more parameters than alert and sound?

Yes. In the Push Notification Programming Guide section The Notification Payload it states

Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean. You should not include customer information as custom payload data. Instead, use it for such purposes as setting context (for the user interface) or internal metrics. For example, a custom payload value might be a conversation identifier for use by an instant-message client application or a timestamp identifying when the provider sent the notification. Any action associated with an alert message should not be destructive—for example, deleting data on the device.

So your payload might look like

{
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
}

Further down on that same page are a number of examples that demonstrate this.

iOS Push Notification custom format

You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:

Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.

So in your case you should do something like:

{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}

Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

NSLog(@"%@",notification['Person']['Address']);

Above will output:

this is a test address

You could find more about custom payloads, along with some examples in Apple docs.

Regards,
HrisTo

Do not display a remote notification based on custom data

Instead I recommend filtering on your server as sending a notification to all devices (just to omit them from being displayed) has down sides including; extra battery drain and content-available notifications do not work when the user swipes away the app.

You can use mutable-content instead which will fire your UNNotificationServiceExtension but this only works on iOS 10 and you still need to take battery into consideration.

You can get the device's OneSignal player id by calling IdsAvailable where you can store this on your back end. Then you can then use include_player_ids on the create notification REST API call to target only the users who should receive the notification.



Related Topics



Leave a reply



Submit