Badge not showing on application icon iOS - ios

I sent a push notification with a badge but it does not increase the number on an app icon. I see one thing under phone notification my app does not have an option for a badge. Anybody knows how I can badge option will appear under application setting in the notification window.

Fix for this If you won't pass UNAuthorizationOptionBadge during requestAuthorizationWithOptions in a setting of your application notification does not show badge option and ultimately you will not receive badge notification whether from server-side you're sending a notification with a badge.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
[[Appboy sharedInstance] pushAuthorizationFromUserNotificationCenter:granted];
}];

Related

Local Notification not triggering after changing device time in iOS

I have local notification setup in one of my application which remind the user regarding medication on a regular basis and n times daily depends on user selection. If the user setup the reminder in the application and changes the date on device setting, reminder not triggering for the next scheduled time. But works fine for the other scheduled times. First notification scheduled is missing. Works fine for normal scenarios. Anyone faced similar issues? TIA. Code snippet as follows:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center setNotificationCategories:[NSSet set]];
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
PRLog(#"Something went wrong");
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
[LocalNotificationShared registerCategories];
}
}];
}
and triggering part like:
NSDateComponents *triggerDate = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:date]; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:NO];
NSString *s = [NSString stringWithFormat:#"%#",#([NSDate timeIntervalSinceReferenceDate])];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:s
content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
PRLog(#"Something went wrong: %#",error);
}
}];
From what I've observed, the time interval for a scheduled notification is independent of the device time. So if you schedule a notification for an hour away, and then set your device time to 59 minutes ahead, it won't matter - you will still have to wait the full hour for it to trigger.

The push notification switch is off in Setting.app after user choose to allow push notification

Did any one ever occur this issue? Should it only be a UI display issue? I mean the Setting.app UI did not refresh it self.
Our "register notification center" codes are as following:
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
if (![FBYPreferences instance].hasRequestAuthorizationForNotification) {
[Taplytics logEvent:#"Accept Notification number" value:#(1) metaData:nil];
}
}else {
[TrackerHelper doTrackEvent:#"Notifiction Not Granted" withCategory:#"Notifiction" withLabel:#""];
if (![FBYPreferences instance].hasRequestAuthorizationForNotification) {
[Taplytics logEvent:#"Decline Notification number" value:#(1) metaData:nil];
}
}
[FBYPreferences instance].hasRequestAuthorizationForNotification = YES;
}];
I look at these codes and think it is OK. Our PM said they can not receive the push notification after they saw the switch is off in Setting.app.
Do you have any suggestion for it. Your idea may solve it and I will really appreciate for it.
Update: we call the above code twice when app launching. Is it possible the reason? We remove the duplicated method call and only call above once.

How do I ask for notifications permission in an IBAction?

In my on-boarding I have a UIPageViewController containing a ‘primer’ screen at the end for authorizing notifications. The user would tap a button labeled “Enable Notifications” and the notifications permission dialog would appear. How do I accomplish this?
You can put:
Objective-C
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.
Swift
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
UIApplication.shared.registerForRemoteNotifications() // you can also set here for local notification.
inside your IBAction.
Please remember also add import UserNotifications for Swift or #import <UserNotifications/UserNotifications.h> for Objective-C in file where you have IBAction and make sure that Push Notification is activated under target - Capabilities - Push notification.
Objective-C:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

How to set sound for push notifications in objective c?

I’m trying to set a sound for push notification in the project which I am developing but it's not working in the device please tell me, how I can set the alert sound for the notification.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog( #"Push registration success." );
}
else
{
NSLog( #"Push registration FAILED" );
NSLog( #"ERROR: %# - %#", error.localizedFailureReason, error.localizedDescription );
NSLog( #"SUGGESTIONS: %# - %#", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
I am using the above code in "Appdelegate.m" in "didFinishLaunch" method can anyone please tell me where I'm going wrong.
For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app. The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app’s data container.
When you send push notification, just add the name of file in JSON payload. Example:
{
"aps" : {
"alert" : "Hey you got a push notification.",
"badge" : 1,
"sound" : "sound.mp3"
}
}
Just put the file (sound.mp3) inside your project bundle (i.e inside the hierarchy of project) and have Copy items if needed option selected while drag and drop.
Hope this will help.

Losing token somehow and notification only goes to phone and not app (post ios10 updates)

I updated my app for ios10, and have the requisite changes in place, forking code between ios10 and below to hit proper method calls.
It seems to work properly. I have a specific app sound that tells me the remote push was processed by the app, as if not, the default device sound is used.
Upon install to test device, all is proper. But once and a while and seemingly randomly, I lose app-centric push receipt at my device.My console never shows the push since it is not sent to the phone.
I run IOS Console to watch it and it seems to report that a "completely unknown" token was received. It works on my pre-ios10 device.
I am at a loss for how to determine what is wrong given all works properly at the start.
I am guessing at the moment that my completion handling might not be correct and that iOS10 is punishing my app?
I will edit the question as i can when i get more clue, but at present, this is very troublesome.
Did you registered for the Push notification ?
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(#"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
return YES;
}
Did you implemented for the below delegate methods ?
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
//Called to let your app know which action was selected by the user for a given notification.
NSLog(#"Userinfo %#",response.notification.request.content.userInfo);
}
And can you check this thread for more clue
iOS 10 and 9 follows different payload structure.
The iOS 10 follows
{
"aps":{
"alert":{
"body":"YOUR_MESSAGE."
},
"badge":1
},
“YOUR_VARIABLE” : "YOUR VALUE"
}
where iOS < 9 follows
{
“aps” : {
“alert” : "YOUR_MESSAGE.”,
“badge” : 1,
“sound” : “default”
},
“YOUR_VARIABLE” : "YOUR VALUE"
}
I thing this might be the issue

Resources