NSNotification Listener iOS - ios

We are asked to implement a feature to listen the notification and decide if it should appear or NOT. Even before they appear on the screen ( NSNotificationCenter or Alert/Banner on Top). We have to set a silent time for users and check if the notification is broadcasted from server the iOS device will not display it if the notification is fired during that silent time.

You can trigger a silent notification by configuring your payload as below.
1.The payload’s aps dictionary must include the content-available key with a value of 1.
2.The payload’s aps dictionary must not contain the alert, sound, or badge keys.
On arrival of the silent push you can check your conditions and trigger a local notification.
May this help you..

You require that Notification already fired from Server,but based on condition you need to stop displaying that.
Apple doesn't support such feature.
This is possible in Android. Here, APNS Server handles all things related to Push Notification.

Related

Are content-available and alert mutually exclusive keys in an APNS aps dictionary

The iOS app I'm working on uses push notifications to deliver messages the user can view in a Message Center. These messages have a set of custom keys outside the aps dictionary that we use to provide different treatments, etc. However, if the user doesn't tap on the notification, the message never appears in the app (server-side messaging not available yet).
So we started adding content-available to the aps dictionary so the app can consume the payload and prepare the message in the message center: the device still receives the notification but now the message is available if they open the app without tapping on the notification. This has been working great in practice.
It looks like this was supported in documentation (Configuring a Background Update Notification section, now archived) but now current docs say "To send a background notification, create a remote notification with an aps dictionary that includes only the content-available key.... You may include custom keys in the payload, but the aps dictionary must not contain any keys that would trigger user interactions." And the documentation page that leads to that says about content-available, "The background notification flag. To perform a silent background update, specify the value 1 and don't include the alert, badge, or sound keys in your payload."
I guess what we're trying to do you could call a Noisy Background Notification, one that updates the app in the background and still has user facing/interactive content.
Are content-available and alert mutually exclusive keys in the aps dictionary?
Thanks!
It appears to me that the intent is to make these mutually exclusive. For example, see:
https://onesignal.com/blog/ios-13-introduces-4-breaking-changes-to-notifications/
and/or research new apns-push-type header which must either be "alert" or "background".
Have you considered using the background mode to process what you need in the background and while doing that, have your app code create a UserNotification based upon the same payload that can be delivered to the device locally to also get the user-visible alert?

IOS Silent Push Notification to send custom Key/Value

I have a use case where I need to send a custom key/value pair. Please see the example below:
{
"aps":{"content-available":1},
"test_data":{"Name":"Vinay"}
}
I have done this exercise at my end but I am not able to send this, Also I have not find a clear proof where Apple restrict this. Can anybody support me on this context.
Yes Apple authorize it the silent push. In fact, Apple explain how to to so:
To support silent remote notifications, add the remote-notification value to the UIBackgroundModes array in your Info.plist file. To learn more about this array, see UIBackgroundModes.
And, in Configuring a Silent Notification:
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.
-
For a silent notification, take care to ensure there is no alert, sound, or badge payload in the aps dictionary. If you don’t follow this guidance, the incorrectly-configured notification might be throttled and not delivered to the app in the background, and instead of being silent is displayed to the user.
Update:
But it seems to be impossible to have a totally silent push AND custom data. It's not well documented, but take a look at this: https://stackoverflow.com/a/36327058/2846494
Source:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW6
https://stackoverflow.com/a/36327058/2846494
there is no issue with this until and unless you have configured well. For configuration details refer apple documentation. I have created a POC and it is working perfectly fine and I noticed only once issue if we are utilising Artisian SDK(older version) so it will swallow your payload and that is the only Issue what I was facing.
To be precise:
We can invoke app in background without notifying user by using silent notification. And we can send our custom data as shown in below example:
{"Data":"Vinay","aps": {"content-available": 1}}

IOS Push Notification disable/enable on the basis of key

Just wondering whether there is any way to disable/enable a push notification when it is received.
For e.g :- When i receive a push notification then i first check in my app whether in notification setting i have enabled or disabled the notification.
There can also be multiple notification settings like
To disable a friend request notification
To disable message notification
So while sending a notification is there any way to append notificationType like if its for friend request or messaging.
Then after checking the notification type and its corresponding setting in the app, showing or discarding the notification.
You can't achieve this just in client side itself. Because once notification arrives it is handled by iOS and displayed in notification centre (or any other type as per user setting). App will not get the notification info, unless it is running.
You can have this as settings in Client and sync it with server to have a check there before pushing the notifications.

push notifications with NO sound JUST in some situations

The app is registered to receive all the 3 kinds of notifications (badge, alert, sound).
I want that push notifications be with NO sound in background JUST IN some situations (in the app user click a button to disable notifications) and to allow the sound for all the other cases.
Do you have some ideas? Thanks
From Apple's Remote Notification Guide:
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.
To support silent remote notifications, add the remote-notification
value to the UIBackgroundModes array in your Info.plist file. To learn
more about this array, see UIBackgroundModes in Information Property
List Key Reference.
Depending on your model (assuming a system like Parse or something), store the user's preference in the cloud and, when generating your PUSH notifications, decide whether to add this flag or not.

Proper use of "content-available" in iOS 7 Push Notifications

I'm just looking for some feedback on my thought process surrounding iOS 7 and the "content-available" key value in a push notification payload.
Scenario:
I force shut down the application. According to Apple because I've done this I will no longer receive any notifications that contain the key value "content-available" in their payload. This means that the alert doesn't show at all, basically nothing happens. No sounds, no alert message, no badge increment.
Theory:
Because of the above scenario it seems as if you'd want to send two push notifications.
A push notification with just your "alert","badge" and "sound" values so that the user sees a notification related to the update irregardless of the application state.
A push notification with just the "content-available" key value. If the app is in a state where it can accept this it does and your background task is performed. In the case it can't accept it the user still receives a visual / audible notification from the first push notification.
Question:
Is this how Apple intends the silent / background notifications to be executed? I don't really see another way that you could implement this.
I think your thought process is right and the information you have is correct for the current implementation of push notifications in iOS 7.0
However people have been filing bug reports and talking about the fact that forcibly quitting the app makes it ineligible to be woken up by a push notification with "content-available" in the payload (See SO answer). Hopefully Apple will address this in iOS 7.1 or at least update the docs.

Resources