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.
Related
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.
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]];
}
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.
I just upgraded my whole iOS push notifications registering for iOS 10, with this code:
-(void)registerForNotifications{
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){
dispatch_async(dispatch_get_main_queue(), ^(void){
if(!error){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
});
}];
}
else {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
}
All my delegates are set in my AppDelegate.
EDIT: I have now been able to further identify the issue. When the app comes back in foreground after notification push, the delegate:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
is only called after about 10-15 seconds, while normally it is obviously called immediately. How is this possible?
I'm now testing push notifications with Localytics and I implement:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
in my AppDelegate for deep linking purposes. When I add breakpoints, I see that this happens:
I receive the push notification correctly, I tap on it, and the app UI freezes for about 10 seconds. Then, finally, didReceiveNotificationResponse is called and the deep linking works.
How can I avoid this huge delay which freeezes the app?
EDIT: it's even worse than I though. When I connect my iPhone to xCode and run a build on my phone, it freezes for ten seconds before working. Then, if I just run the exact same build without running it on xCode (so without breakpoints), the app freezes for 10 seconds and then crashes.
EDIT: here is a screenshot of my main thread when I pause on xCode while it freezes:
There's something weird in your stacktrace. semaphore_wait_trap, semaphore_wait_slow. Try seeing here and here and here. That being said my guess is that you're calling your (void)registerForNotifications from a wrong thread and it's causing this issue.
Additionally I don't understand why you have a dispatch_async(dispatch_get_main_queue in your implementation. It seems unnecessary. Try seeing answers from here
I use APNS and it's work fine on iOS 9.
With the new push API changes on iOS10 i cant register for push notification so i insert the next changes:
Enable push notification in the target capabilities tab.
In didFinishLaunchingWithOptions we check the OS version and register as followed :
if (SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(#"10.0")) {
//ios 10 Notifiction
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(#"iOS 10 push notification register successfully ");
}
}];
} else {
// iOS 8-9 Notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(#"iOS 9 push notification register successfully ");
}
With this changes I manage to perform registration in iOS 9 and iOS 10 but I have couple of problems :
Once I enable the push notification in the target capabilities tab the push notification stop working on iOS 9 although the registration was complete successfully .
The push doesn’t work on iOS 10 at all although the registration was complete successfully .
Please keep in mind that if I turn off the push notification in the target capabilities tab (with the same code) the push return to work on iOS 9 but I cant register for APNS on iOS 10.
if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings = UIUserNotificationSettings (types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}else {
let types: UIRemoteNotificationType = [.alert, .badge, .sound]
application.registerForRemoteNotifications(matching: types)
}