2

I am trying to send a json format which doesn't have an "alert" attribute. The thing is, when I try to remove the alert attribute, the notification won't appear. Is there any way I can handle this? Thanks in advance

P.S I've tried to use action, however it still doesn't show up (I think this is only possible in android)

Rye
  • 2,273
  • 9
  • 34
  • 43

1 Answers1

2

Yes, you can do it. It is possible to send a push notification without an alert. You can even register your application just to badge notifications, in which case the provider server won't even be able to send alerts or sounds.

The Notification Payload

Each push notification carries with it a payload. The payload specifies how users are to be alerted to the data waiting to be downloaded to the client application. The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this limit. Remember that delivery of notifications is “best effort” and is not guaranteed.

For each notification, providers must compose a JSON dictionary object that strictly adheres to RFC 4627. This dictionary must contain another dictionary identified by the key aps. The aps dictionary contains one or more properties that specify the following actions:

An alert message to display to the user

A number to badge the application icon with

A sound to play

Note that it says one or more of the properties. The alert property is optional. You can even send a notification with an empty aps dictionary (i.e. send only custom properties).

The following example shows an empty aps dictionary; because the badge property is missing, any current badge number shown on the application icon is removed. The acme2 custom property is an array of two integers.

{

"aps" : {

},

"acme2" : [ 5,  8 ]

}

The only alert the user will see it the alert that asks him/her whether to allow push notifications. That alert will only be displayed the first time the app is launched after installation.

In the below example you register to non alert notifications (badges and sounds only) :

Registering for remote notifications

- (void)applicationDidFinishLaunching:(UIApplication *)app {

 // other setup tasks here....

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

}



// Delegation methods

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

const void *devTokenBytes = [devToken bytes];

self.registered = YES;

[self sendProviderDeviceToken:devTokenBytes]; // custom method

}



- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

NSLog(@"Error in registration. Error: %@", err);

}

Hope this will help you.

Irfan
  • 4,301
  • 6
  • 29
  • 46
  • very clear info thank you. Although once the "alert" attribute was taken out from the json. The instance that was sent to the device will not be able to prompt on the notification center? – Rye Mar 10 '14 at 07:54
  • You welcome, kindly explain your scenario what functionality you exactly want? – Irfan Mar 10 '14 at 07:59
  • Actually I already made a format for sending push to android, see sample below: { "action": "com.sample.pAction", "header": "My Title", "letter": "My Message" } However, I may not be able to use this format, since in ios it might need an "alert" to be able to render in the pull down notification area in ios. – Rye Mar 10 '14 at 08:04
  • You can also do this by sending alert atribute with empty string as: { aps = { "content-available" = 1; "alert" = ""; }; } – Irfan Mar 10 '14 at 08:04
  • Kindly also check this link may it will help you: http://stackoverflow.com/questions/19239737/silent-push-notification-in-ios-7-does-not-work – Irfan Mar 10 '14 at 08:06
  • Ok,if my answer is helpful for you then kindly accept my answer so, this info will may helpful others too. Thanks. – Irfan Mar 10 '14 at 08:12