Application icon badge number not increasing : Xcode - objective-c

I am facing a problem with push notification application badge number value updation.
I am doing like:
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
// do stuff when app is active
}else{
// do stuff when app is in background
[UIApplication sharedApplication].applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber+1;
/* to increment icon badge number */
}
}
But, the icon is showing the badge number as '1' always, and it is not incrementing when more notifications are there/ one notification came after another.
Any advice is appreciable...

The badge number is set by the operating system when you receive a JSON notification payload that resembles the following:
{
"aps" : {
"alert" : "New notification!",
"badge" : 2
}
}
As you see, it's the server who is responsible for setting the correct number in the badge key. Your server needs to track or compute the number of pending notifications for each user and generate the badge number before sending the notification to Apple.
The client responsibility is to clear the notification badge, or decrement it, when the user sees a notification. The code to do so is
application.applicationIconBadgeNumber = application.applicationIconBadgeNumber - 1; // Decrement counter
or
application.applicationIconBadgeNumber = 0; // Reset counter assuming the user is able to see all notifications at once.

you can create a static variable instead, and assign it to the applicationIconBadgeNumber:
static int i=1;
[UIApplication sharedApplication].applicationIconBadgeNumber = i++;

I had the same problem and solved it by creating int. variable in class.h
like this :
a custom class.H
#property int badge;
a custom class.M
-(id)init{
_badge=0;
self = [super init];
if (self) {
}
return self;}
-(void)reminderCreator:(NSDate*)onTime withText:(NSString*)text{
_badge += 1;
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = onTime;
localNotification.alertBody = text;
localNotification.soundName=UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber=_badge;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }
so if you initialize this custom class, somewhere (maybe at your viewController) and then call the reminderCreator method several times to setup few localNotifications, it will assign incremented number to each notification.
+1 if this helped :)

The -(void)application:didReceiveRemoteNotification: will only be called when your app is running in the foreground.
If you want the badge to be increase when your app isn't running you should set the badge number in the push notification payload.
You should there for keep track of the badge number server side, since the badge property of the push notification payload will be use as the badge number. It will not increment the badge number for you.
Since the system handles the incoming push notifications your app is not informed of received push notifications for you app. Only when you app is running in foreground will the -(void)application:didReceiveRemoteNotification: be called. There is no way to get you app to repsond to push notification when it is not in the foreground.

Yes it seems that it always returns badgeNumber 1 in spite it has value increased to 2.
when using such code:
// update badge count
let currentBadgeCount = UIApplication.shared.applicationIconBadgeNumber
let badge = (aps["badge"] as? Int) ?? 1
UIApplication.shared.applicationIconBadgeNumber = currentBadgeCount + badge
so I think the only solution will be to store it in UserDefaults and then update value of UserDafaults and then it is set then update badge count?
This should work:
static var notificationsCount: Int {
set {
let userDefaults = UserDefaults(suiteName: SharedAppData.appGroup)
userDefaults?.set(newValue, forKey: "notificationsCount")
UIApplication.shared.applicationIconBadgeNumber = newValue
}
get {
let userDefaults = UserDefaults(suiteName: SharedAppData.appGroup)
return userDefaults?.integer(forKey: "notificationsCount") ?? 0
}
}
And then
SharedAppData.notificationsCount = 0 // to reset badge count
And this to increment
// update badge count
SharedAppData.notificationsCount += 1

Related

Enable/disable a perticular UILocalNotification and set multiple notification once

NOTE: Need to know how to Set a number of alarm or notification in between two given dates regularly?
I have to create an app where,i have to enable/disable multiple UILocalnotification set in a tableview.'
If i choose a date i have to then set time on that perticular date and also need to set the notification before timePlay (5,10,15,20 min prior).
And End date: the date until when the notification plays regularly.
How to set all the notification for a perticular notification ID at once?
How to disable a perticular notification?
ALSO PLEASE TELL ME : CAN HOW CAN I SET UILOCALNOTIFICATION using database?
I have create a database having
Notification ID //unique id of notification
Notification Name //notification title
Time1 //can set five time the notification will show
Time2(optional)
Time3(optional)
Time4(optional)
Time5(optional)
Before timePlay//can show the notification before time of notification 1 to 5 above
Start Date // the date from which the notification start
End Date //the date when the notification stops.
i can set simple notification like this
// UILocalNotification properties:
// alertBody - the message displayed in the notification
// alertAction - if notification is displayed as an alert, this is the label of the action button, if not specified, "Launch" will be used
// soundName - the name of a sound file (AIFF, CAF or WAV) to be played when the notification appears, if not specified, no sound will be played. To use the default sound UILocalNotificationDefaultSoundName can be provided.
// userInfo - you can pass an NSDictionary object with additional data to be used by our app after the notification fires (optional)
// fireDate - an NSDate object specifying the date and time for the local notification to be fired (obligatory)
// timeZone - an NSTimeZone object. If specified, the fireDate is measured against the user's local time zone, if not against universal time
// repeatCalendar - the calendar (NSCalendar) the system should refer to when it reschedules a repeating notification
// repeatInterval - the calendar interval (NSCalendarUnit) at which to reschedule the notification, the default is 0, which means don't repeat
// alertLaunchImage - will be presented when your app is run or summoned from the background
// Create a new local notification
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.alertBody = #"Wake up! Its tuition time!";
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:60]; // 60 seconds
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber += 1;
Try scheduling which will enable the notification and use cancel which will be disable the notification:-
-(void)enableNotification
{
[self cancelAlarm]; //clear any previous alarms
UILocalNotification *alarm = [[UILocalNotification alloc] init];
alarm.alertBody = #"alert msg";
alarm.fireDate = [NSDate dateWithTimeInterval:alarmDuration sinceDate:startTime];
alarm.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.name forKey:kTimerNameKey];
alarm.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
-(void)disableNotification{
for (UILocalNotification *notification in [[[UIApplication sharedApplication] scheduledLocalNotifications] copy]){
NSDictionary *userInfo = notification.userInfo;
if ([self.name isEqualToString:[userInfo objectForKey:kTimerNameKey]]){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
at time of notification schedule give a unique notification key in notification's userinfo and to cancel notification you can use below code as where
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:#"%#",[userInfoCurrent valueForKey:#"uid"]];
if ([uid isEqualToString:uidtodelete])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}

Stop local notification

I have a problem below:
When minimize app to background by press home button, create a local notification for pop-up every 5 minutes.
Remove app from background.
-->My expected then pup-up just only show when app exist and it's discarded when remove app from background.
My issue that local notification still active and it still showing pop-up every 5 minutes after remove it from background.
How can i stop it?
Please help me!
Thanks in advanced.
Put this in application delegate. It will remove all local notifications when the application enters background.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
If you don't want to cancel all notifications... I've set up a unique identifier stored in the notification's userInfo dictionary. When I want to delete I fast enumerate through all notifications and pick out the correct one for deletion.
My stumbling blocks here were remembering to store the UUID I'd created for the notification and also remembering to use isEqualToString in the fast enumeration. I guess I could also have used a specific name string instead of a unique identifier. If anyone can let me know a better method than fast enumerating please let me know.
#interface myApp () {
NSString *storedUUIDString;
}
- (void)viewDidLoad {
// create a unique identifier - place this anywhere but don't forget it! You need it to identify the local notification later
storedUUIDString = [self createUUID]; // see method lower down
}
// Create the local notification
- (void)createLocalNotification {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
localNotif.fireDate = [self.timerPrototype fireDate];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Hello world";
localNotif.alertAction = #"View"; // Set the action button
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:storedUUIDString forKey:#"UUID"];
localNotif.userInfo = infoDict;
// Schedule the notification and start the timer
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
// Delete the specific local notification
- (void) deleteLocalNotification {
// Fast enumerate to pick out the local notification with the correct UUID
for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[localNotification.userInfo valueForKey:#"UUID"] isEqualToString: storedUUIDString]) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
}
// Create a unique identifier to allow the local notification to be identified
- (NSString *)createUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (__bridge NSString *)string;
}
Most of the above has probably been lifted from StackOverflow at sometime in the last 6months. Hope this helps

How to set badges on app icon when app is in minimize mode?

When application is in minimize state and at the same time notifications come then badges should be see on app icon.
If a notification arrives and your application is not is foreground, the OS handles the notification.
Your notification can have a field badge that will make the OS update the badge. However, this means that the server than sends the notification must have a way of knowing which number should be the badge.
The notification body would look like this:
{
"aps" : {
"badge" : 9
}
}
Heres my solution. I needed to achieve the same thing in an app that I did. I had a queue of Downloads and wanted to use a Badge to show how many downloads were left, and keep updating it even while in the background. Basically my solution was, every time one of the downloads were completed I setup a UILocalNotification with a silent sound, and no message text. Simply just set the badge..As seen below..
- (void)queRequestFinished:(ASIHTTPRequest *)request {
self.inResourceCount -= 1; // Deducted 1 from the total count of downloads in queue.
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [NSDate date]; // Schedule notification for now.
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.soundName = #"silence.caf";
notif.applicationIconBadgeNumber = inResourceCount; // Number you want displayed as Badge.
// This is where the magic happens, and actually changes your badge.
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
Id like to point out, that my scenrio may be different from yours. I was using ASIHTTPRequest library, which has support for continuing downloads while backgrounded, and the method above queRequestFinished: is called even while in the background. Hope this helps, if it does mark it as the answer :) .

How BarAlarm iOS app is made? [duplicate]

I am developing a non-appstore app for iOS. I want to read the cellular signal strength in my code.
I know Apple doesn't provide any API by which we can achieve this.
Is there any private API that can be used to achieve this? I have gone through the various threads regarding this issue but could not find any relevant info.
It is completely possible because there is an app in the app-store for detecting the carrier's signal strength.
Get signalStreght IOS9:
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:#"statusBar"] valueForKey:#"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(#"UIStatusBarSignalStrengthItemView") class]])
{
dataNetworkItemView = subview;
break;
}
}
int signalStrength = [[dataNetworkItemView valueForKey:#"signalStrengthRaw"] intValue];
NSLog(#"signal %d", signalStrength);
It is not very hard.
Link CoreTelephony.framework in your Xcode project
Add the following lines where you need it
Code:
int CTGetSignalStrength(); // private method (not in the header) of Core Telephony
- (void)aScanMethod
{
NSLog(#"%d", CTGetSignalStrength()); // or do what you want
}
And you are done.
Update May 2016
Apple removed this opportunity.
I briefly looked at the VAFieldTest project located at Github.
There seems to be getSignalStrength() and register_notification() functions in Classes/VAFieldTestViewController.m that might be interesting to you as they call into CoreTelephony.framework.
I am pretty confident that some of the used calls are undocumented in the CoreTelephony framework documentation from Apple and therefore private - any app in the AppStore must have slipped passed inspection.
To get signal streght in iOS 9 or above in Swift 3, without using the private API from CoreTelephony - CTGetSignalStrength(). Just scouring the statusBar view.
func getSignalStrength() -> Int {
let application = UIApplication.shared
let statusBarView = application.value(forKey: "statusBar") as! UIView
let foregroundView = statusBarView.value(forKey: "foregroundView") as! UIView
let foregroundViewSubviews = foregroundView.subviews
var dataNetworkItemView:UIView!
for subview in foregroundViewSubviews {
if subview.isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
dataNetworkItemView = subview
break
} else {
return 0 //NO SERVICE
}
}
return dataNetworkItemView.value(forKey: "signalStrengthBars") as! Int
}
Attention: If the status bar is hidden, the key "statusBar" will return nil.
I haven't tested it, but apparently this is now a method of CTTelephonyNetworkInfo instead of a global/static function.
The return type is id, so I think you get either a NSDictionary (as the _cachedSignalStrength ivar implies) or an NSNumber (as the old function implies).
id signalStrength = [[CTTelephonyNetworkInfo new] signalStrength];
This changed in iOS 8.3, as you can see from the commit.
Note that this is still not documented! So if your app will go in the App Store, take your precautions.
Here's Lucas' answer converted to Xamarin, and tested on iOS 10.2.1:
var application = UIApplication.SharedApplication;
var statusBarView = application.ValueForKey(new NSString("statusBar")) as UIView;
var foregroundView = statusBarView.ValueForKey(new NSString("foregroundView")) as UIView;
UIView dataNetworkItemView = null;
foreach (UIView subview in foregroundView.Subviews)
{
if ("UIStatusBarSignalStrengthItemView" == subview.Class.Name)
{
dataNetworkItemView = subview;
break;
}
}
if (null == dataNetworkItemView)
return false; //NO SERVICE
int bars = ((NSNumber)dataNetworkItemView.ValueForKey(new NSString("signalStrengthBars"))).Int32Value;

Apple push notification badge number

I have developed server side application to maintain the badge number as increment or decrement after receiving new notification and delete after seeing notification it works fine.
But there is some problem in showing the badge, the actual scenario is - After getting new notification on device, I am click on cancel button then badge number shows correctly but after that I will open the application and close the application badge will be removed. That means I am not sending request to the server that notification was seen by me and now you can decrement the badge by one. Then also badge removed from app icon.
My question is that when we open the application then badge number automatically removed from (application) device? or it will shows as it is until we set to zero?
It will show until you set it to zero and you can do it with the following code:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
EDIT:
It is more common to set the badge number as you receive the notification, in either application:didReceiveRemoteNotification: or application:didFinishLaunchingWithOptions: methods of your UIApplicationDelegate class.
You can read more about it in the Local and Push Notification Programming Guide
If you want to change the icon badge automatically use the following code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
application.applicationIconBadgeNumber = 0;
NSLog(#"userInfo %#",userInfo);
for (id key in userInfo) {
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
}
[application setApplicationIconBadgeNumber:[[[userInfo objectForKey:#"aps"] objectForKey:#"badge"] intValue]];
NSLog(#"Badge %d",[[[userInfo objectForKey:#"aps"] objectForKey:#"badge"] intValue]);
}
We also need to change the php file. So we can get the change the icon badge automatically
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'id' => '135',
'badge' => 8
);