change Badge and push notification in iPhone SDK - iphone

I tried the push notification tutorial . It's working fine but problem is badge. When I click on the view, app is appear and the close it. it still red badge in app icon. How to remove it ?
Another question is
when I click the view, it will appear home screen. I want to show other view when coming from push notification.

This will reset the application badge number. If you set that value to zero, it will hide the badge.
[UIApplication sharedApplication].applicationIconBadgeNumber = iCount;
To handle your push notification with a separate view, you need to handle the following message in your application delegate:
- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
You can access the userInfo dictionary to get additional information about the push notification that resulted in the message callback.

Related

iOS Push Notifications - update badge without alert?

Is there a way to update the number in the badge without showing an alert or opening the app?
I am writing an app that should always display the current number of unread messages in the icon badge, but I want to do so without displaying any alerts to the user.
I am developing for iOS 5.0+.
EDIT: To be more clear, I am asking about a way to do this when the app is not running. I want the server to push a new badge number without showing an alert.. Is this possible?
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).
Example 5. 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 this example you register to non alert notifications (badges and sounds only) :
Listing 2-3 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);
}
All quotes are taken from the Apple Local and Push notifications programming guide.
you should use applicationIconBadgeNumber for locally handling your app badge number
[UIApplication sharedApplication].applicationIconBadgeNumber = number_of_notifications;
I don't think it is possible to do without alert as far as adding badge counter from remote notification. You should read about APN Service, in your case you might register for UIRemoteNotificationTypeBadge you should read about Local & Push Notification Programming guide
You can use
[UIApplication sharedApplication].applicationIconBadgeNumber = aNumber;
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
use this method....this will help u.

Clear push notification badge after increment

I am working on push notification in iphone. when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. if i open application its 0. Next time its should be 1,2,3,4... but its showing last number and +1. i want reset push notification badge after open application. i am sending +1 from urban airship.
and its not working for me.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
I use this code in my app because the Urban Airship (UA) documentation said to
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UAPush shared] resetBadge];
but it doesn't work, the badge on the app icon keeps incrementing. I saw a few posts on this very issue on UA's forums and they haven't given a clear answer.
EDIT #1:
I received the following response from a support technician at UA with the following suggestions, which worked great:
What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following:
[[UAPush shared] setAutobadgeEnabled:YES];
[[UAPush shared] resetBadge];//zero badge on startup
and also call [[UAPush shared] resetBadge]; in the following methods as well:
applicationDidBecomeActive:(UIApplication *)application
and
didReceiveRemoteNotification:(NSDictionary *)userInfo
The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. Still works beautifully.
EDIT #2:
I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
{
appState = application.applicationState;
}
[[UAPush shared] handleNotification:userInfo applicationState:appState];
[[UAPush shared] resetBadge];
}
because I was having a problem with the following scenario:
User is in app
Push notification received
No badge was displayed on app icon when returning to home screen (as expected)
Another push notification is received
Badge displayed number greater than 1
With the modification above, this scenario is handled. I guess you have to tell UA that the notification is handled when one is received and the app is running in the foreground.
I have used Urban Airship and have had this problem before. You are not showing the code but i am assuming that when a notification is received, you are setting the application badge number to what urban airship is passing to you, don't do that. Just let the application self handle this, when a remote notification comes in, let it auto increment on its own. If that is not the case it could be that, on the urban airship side, you are setting a badge number to send with the push. Do not send a badge number with the push notification, leave that part out, iOS should auto increment your badge from its current badge number.

iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}

Can i get info about notification (apn)

Can I get list of push notifications (APNS notifications) for my app when app became from background to foreground mode?
In foreground mode i can receive info about push notification in callback
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Other case:
My app receive push notification, when app in background mode.
After this i click on app icon, and i want to get info about received notification. How can i get this info?
If i click directly on the notification (not on app icon), in background mode, then callback didReceiveRemoteNotification is call.
Can I get list of push notifications (APNS notifications) for my app when app became from background to foreground mode?
No. There is no list. You can only get one notification at once. When the users iPhone is offline and you send 5 notifications the user will only get the last one you have send.
If the user starts your app using the open action on the notification you will get it on start using:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Other Case
My app receive push notification, when app in background mode. After this i click on app icon, and i want to get info about received notification. How can i get this info?
You can't. When the user closes the notification and opens you app later it is already gone and there is no way to access it.
When you send push notifications you probably have a server reachable over the internet, where you register the devices of the user.
The usual way to handle this is to store the notifications on this server and query it on app launch... so use the notification just to notify the user to start your app and then check your server on launch of the app for whatever you want.
Once your application reaches foreground and actively running, the notification alerts like sound, alert won't be displayed or you wont be notified.
But you will get a call back in the UIApplication Delegate that you can make use of it.
The api is,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
And, if your application is running in background, then the notification appears and only when you click on "View" button, you will get the call back in UIApplication delegate.
If you click on Close button, you won't get the call back in the application.

Push Notification Problem

I want to get the reminder at each notification comes from the web service in the application for the different tasks. For that what i have used the following methods.
but i am not getting push notific
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
Receiving push messages in the app
So what happens when your iPhone receives a push notification? There are three possible situations:
The app is currently running in the foreground. Nothing happens on the screen and no sound is played, but your app delegate gets a notification. It is up to the app to do something in response to the notification.
The app is closed, either the iPhone’s home screen is active or some other app is running. An alert view pops up with the message text and a sound is played. The user can press Close to dismiss the notification or View to open your app. If the user presses Close, then your app will never be told about this notification.
The iPhone is locked. Now also an alert view pops up and a sound is played but the alert does not have Close/View buttons. Instead, unlocking the phone opens the app.
I ran into a lot of problems on Push notification. Please note: if you are not getting the device token back, check to make sure the device is connected to the internet. If not device cannot contact the APN server. It took me days to find this out