show alert of push notification dynamically on ios - ios

In android we can make alert message of push notification dynamically on conditions(data) which comes in push notification payload. i.e piece of code gets executed when push comes and then alert is shown.
Cant we do this in ios? Do we need to send alert message from API all the time? cant we change it on client side(ios)?

In fact, you can. You just need to send a "silent" remote notification, handle the notification in your app and display local notifications depending on the payload. The steps are:
Implement didReceiveRemoteNotification:fetchCompletionHandler:
Make sure to register for remote notifications, see documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Also make sure to edit Info.plist and check the "Enable Background Modes" and "Remote notifications" check boxes:
Additionally, you need to add "content-available":"1" to your push notification payload, otherwise the app won't be woken if it's in the background (see documentation here):
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app
So payload should at least look like this:
{
aps = {
"content-available" : 1,
sound : ""
};
}
Just leave the sound property empty and omit the alert/text property and your notification will be silent.
Unfortunately, the app won't be woken up, if it's not running at all (force-quit), see this answer.

Related

iOS: Making push to run a method without displaying a notification

Is it possible to in iOS to get a push from the server and then to run a method without displaying a notification at all? For example, to check if now is the configured time and if it is to dispaly a notification and if not to send the server a non availble message.
Yes, what you are looking for are silent push notifications which got introduced in iOS 7. Take a look at the docs regarding push notifications which state:
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.
and
content-available -
number -
Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed, application:didReceiveRemoteNotification:fetchCompletionHandler: is called.
(Newsstand apps are guaranteed to be able to receive at least one push with this key per 24-hour window.)
You can then react accordingly and eventually actually trigger another notification if you feel like the user needs to be informed about something (not 100% sure about that one though).
A little more in-depth information and code can be found in this objc.io post.
Yes, it is possible:
Implement didReceiveRemoteNotification:fetchCompletionHandler:
Make sure to register for remote notifications, see documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Also make sure to edit Info.plist and check the "Enable Background Modes" and "Remote notifications" check boxes:
Additionally, you need to add "content-available":1 to your push notification payload, otherwise the app won't be woken if it's in the background (see documentation here):
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app
So payload should at least look like this:
{
"aps" = {
"content-available" : 1,
"sound" : ""
}
}
Just leave the sound property empty and omit the alert/text property and your notification will be silent.
Unfortunately, the app won't be woken up, if it's not running at all (force-quit), see this answer.

Apple Push Notification in Background Issue

I'm using parse.com as my APNs provider for a test app I'm building. I've confirmed that push notifications are working as I have been able to successfully receive alerts when the app is in the foreground. Also, I have the remote-notification value for the UIBackgroundModes key specified in my plist.
In my app, I'm wanting to send a user's current location data back to my app-specific parse.com database when a push notification is received. I don't actually care about the notification payload itself, as the notification is just a means to getting a small piece of info. The app is constantly collecting data in the background and storing it in a persistent NSDictionary.
I've put the location sending code in the application:didReceiveRemoteNotification: method. If my app is in the foreground when I receive a notification, the method gets called. If my app is in the background, the method isn't called.
Am I doing something wrong?
Is it even possible to execute an API request in application:didReceiveRemoteNotification: when the app is in the background and the user hasn't interacted with the notification?**
EDIT: The problem persists even when I use application:didReceiveRemoteNotification:fetchCompletionHandler:.
Note: My NSDictionary full of location data isn't empty. Also, I am not attempting to do any UI manipulation in the background - just trying to perform an API request.
Check the following:
Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
The documentation for UIApplicationDelegate -application:didReceiveRemoteNotification is pretty clear on this method:
If the app is not running when a push notification arrives, the method
launches the app and provides the appropriate information in the
launch options dictionary. The app does not call this method to handle
that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
You need to handle notifications in both methods to be able to respond when your app is in the background and foreground.
you can try to use this instead of
application:didReceiveRemoteNotification
method, since you need to fetch your push in background mode, thus, this would works when the app is in background mode. However, you might need to add in custom notification or UIAlertView when app is in foreground to display your message. Hope it helps and it's not too late.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Remote Notification Received: %#", userInfo);
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"message to be displayed";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
completionHandler(UIBackgroundFetchResultNewData);
}
Alex L's answer almost worked for me.
Just make sure the value of content-available is 1 (number), not a string "1":
{
"alert":"",
"content-available":1,
"sound":""
}
badge parameter is optional.
(Using Parse Server for APNs)

Receive Push Notification in background and create a local Notification without the user knowledge

I am receiving a remote push notification, and i need to register a UILocalNotification, within 30 minutes, but i need to do it even if the user do not click in the notification, in the background.
There's any way to do it, like in :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
It works fine here, but only if the user clicks in the notification, or if the app is currently running.
You can't handle a remote notification if the application is not running until the user doesn't launch your app. You should deal with the business logic on server-side. Keep track of when the notifications are sent and trigger the appropriate action after the time frame are exceeded.
You need to turn on "background fetch" in the "capabilities" section of your project settings, and use the application:didReceiveRemoteNotification:fetchCompletionHandler delegate method in your ApplicationDelegate file. This way you can handle remote notifications in the background. Don't forget to set the content-available flag in your aps dictionary when sending the push.

Receiving Push Notifications while in background

I know this is covered in a lot of places, but I cannot figure this out. I use Urban Airship for push notifications. Everything seems to be fine except that when my app is in the background - didReceiveRemoteNotification is not getting called. It works when in the foreground - I am able to properly process messages. And I am able to get to messages from launch options if message is tapped from notifications center. But when in the background, a message it send - iOS displays the alert - didReceiveRemoteNotification is not called. When I tap on my Application icon (not from within notification center) the app comes to the foreground and I have no idea the notification is present. Any ideas?
application:didReceiveRemoteNotification: will call in the background only when you have added content-available key with value 1 into the notification payload.
In case of the Urban Airship, you can send Test Push under the Setting tab.
Sample Payload for Push Notifications:
{
"aps": {
"alert": "aaaa",
"badge": "+1",
"content-available": "1"
},
"device_tokens": [
"86BA71E361B849E8312A7B943BA6B26A74AB436381CF3FEE3CD9EB436A12A292"
]
}
Apple has clearly mentioned in his documentation....
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app.
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
From the APNS programming guide :
Let’s review the possible scenarios when the operating delivers a
local notification or a remote notification for an application.
The notification is delivered when the application isn’t running in the
foreground. In this case, the system presents the notification,
displaying an alert, badging an icon, perhaps playing a sound.
As a result of the presented notification, the user taps the action button
of the alert or taps (or clicks) the application icon. If the action
button is tapped (on a device running iOS), the system launches the
application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it
passes in the notification payload (for remote notifications) or the
local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the application
calls the same method, but furnishes no information about the
notification.
I believe the last sentence describes your case, and explains why your application gets no information about the notification.
didReceiveRemoteNotification is calling ONLY if app in foreground or if app is just launched or is bought from background to foreground
link in Apple and some question
Method didFinishLaunchingWithOptions:(NSDictionary *)launchOptions parameter launchOptions one of the dictionary keys are UIApplicationLaunchOptionsRemoteNotificationKey which holds the pressed push notification info.
You can push received info after tour main root controller is initialised. I save it to some property and then push it after view is initialised.
if (launchOptions) {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
self.notificationToMakeAction = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
}
}

Remove "remote notifications" from notification center

I have the following issue: "My app receive some remote notifications from an own server just to show to the user some practical information. I am not using a icon badge, because I don't need it. If the application user touch the remote notification from the iOS Notification Center my application can catch it without any problem, I receive the options from application:didFinishLaunchingWithOptions: or, if the application is open I catch the remote notification with application:didReceiveRemoteNotification: selector. But, now I want to remove these notifications from iOS Notification Center because It is just a message and I have been looking for the answer in another posts and I've tried these solutions in my app and they don't work"
Some solutions were the next:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[application cancelAllLocalNotifications];
application.applicationIconBadgeNumber = 0;
...
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
if (launchOptions) {
[application cancelAllLocalNotifications];
application.applicationIconBadgeNumber = 0;
}
...
}
And the remote notification still in the iOS Notification Center, how can I remove from that place without any tricky code or is an iOS SDK issue? I don't think that an issue was possible because Tweetbot app remove its remote notifications from iOS Notification Center after you enter to the app.
Regards!
With introduction of UNUserNotificationCenter for iOS 10 and above, it is now possible to remove few or all remote notifications of your app.
UNUserNotificationCenter documentation
With a shared singleton instance of this class, it is possible to manage delivered remote notifications on the device. Specifically, the following methods can be used:
func removeDeliveredNotifications(withIdentifiers: [String]) if you want to remove specific notification of your app, OR func removeAllDeliveredNotifications() to remove all notifications of your app.
First of all make sure you haven't set badge notification to be off in the control panel (I noticed that if the badge has a number to begin with, then badging is turned off in the control panel, it cannot be set to 0).
If its not turned off then in addition to setting applicationIconBadgeNumber to 0, also try canceling all local notifications (even if you haven't queued any, if you have first get a list of them, then cancel them, then register them back again). Yes clearing local notifications can have an effect on being able to clear the badge number for remote notifications.

Resources