How to set sound for push notifications in objective c? - ios

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.

Related

Badge not showing on application icon 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];
}];

Allow push notification popup not displayed

I have developed an app for both Android and IOS to receive push notifications, using phone gap push plugin. when I deploy app on Android device, I'm able to receive push notifications. But when I deploy app on IOS and run app for the first time. I'm not getting the popup "Allow push notification", which will give permission for the app to receive push notification. I want to know if anybody has faced this problem earlier or any ideas to fix this issue.
I followed several posts online, but couldn't find any thing related to this. As per my understand popup should be displayed by plugin by default.
Thanks in Advance.
If you use Objective-C, use below code:
// AppDelegate.m
#import UserNotifications;
After that, in method "didFinishLaunchingWithOptions" add this code:
if( SYSTEM_VERSION_LESS_THAN( #"10.0" ) )
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
if( optind != nil )
{
NSLog( #"registerForPushWithOptions:" );
}
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
NSLog( #"Push registration success." );
}
else
{
NSLog(#"Something went wrong");
}
}];
}
If you use swift, read this https://useyourloaf.com/blog/local-notifications-with-ios-10/ . I think it helpful for you.

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.

iOS - Adding actions to remote notifications. What am I missing?

I'm trying to add an action to the iOS remote notification.
This is the command I'm sending to houston (https://github.com/nomad/houston)
apn push "APNS_TOKEN_HERE" -c /Users/johannwerner/Desktop/apple_push_notification.pem -P '{"aps":{"alert":{"action-loc-key":"OK","title":"hello","body":"Hello"},"category":"ACTIONABLE"}}'
When the app launches from the push notification and I log launchOptions from the didFinishWithOptions method I get the following
{
UIApplicationLaunchOptionsRemoteNotificationKey = {
aps = {
alert = {
"action-loc-key" = OK;
body = Hello;
title = hello;
};
category = ACTIONABLE;
};
};
}
When I'm registering the notification in the code I'm using the following.
if (NSClassFromString(#"UNUserNotificationCenter")) {
UNNotificationAction* snoozeAction = [UNNotificationAction
actionWithIdentifier:#"SNOOZE_ACTION"
title:#"Snooze"
options:UNNotificationActionOptionNone];
UNNotificationCategory* generalCategory = [UNNotificationCategory
categoryWithIdentifier:#"GENERAL"
actions:#[snoozeAction]
intentIdentifiers:#[#"ACTIONABLE"]
options:UNNotificationCategoryOptionCustomDismissAction];
UNAuthorizationOptions options = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound |UNAuthorizationOptionAlert);
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObjects:generalCategory, nil]];
center.delegate = self;
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error ){
[Localytics didRequestUserNotificationAuthorizationWithOptions:options
granted:granted];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
[[NSNotificationCenter defaultCenter] postNotificationName:PUSH_DELEGATE_AFTER_REGISTERED object: nil];
}];
}
I'm getting a push that has my title and body text of "hello" but there is no button or action on the push notification.
I'm been through this guide https://nrj.io/simple-interactive-notifications-in-ios-8/
as well as apples documentation
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1 for the payload
and https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SupportingNotificationsinYourApp.html for adding actions to the notification.
Finally found it. In my payload I was sending category as string "actionable". It should be "GENERAL"

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