Call a function once user handles push notification registration alert - objective-c

These codes will be called when a page loads.
UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
Now I would like to present the next view controller as soon as the user handles the alert. Is it possible? Any help will be greatly appreciated.
Thanks.
Update
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(go)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (IBAction)buttonPressed:(id)sender
{
UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
- (void)go
{
TutorialViewController *tvc = [[TutorialViewController alloc] init];
[self presentViewController:tvc animated:YES completion:nil];
}
This seems to work. I'm wondering if the code will be efficient?

It's a bit tricky, but it's possible. The alert for notification permissions is a system alert, which means your app resigns active when it's shown, and you get a application:didBecomeActive call when the user dismisses it.
So the basic technique is to set a flag in your app delegate when you're about to trigger the alert. In your application:didBecomeActive implementation, check the flag and perform whatever work you need to do.
This is not 100% foolproof, as the app might be resigning active for a different reason. You can mitigate some of this by also tracking whether the app went to the background after resigning active. You should also be sure that the alert will be shown when you ask for permissions. If the user has previously answered, no alert is shown.

When The App is brought into running state via Notifications..This method is fired
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
You can then retrieve the notification received from the launchOptions dictionary and open the appropriate screen.
Hope this helps.

Related

Admob close button is upper the safe area on iPhone 12

Using Google-Mobile-Ads-SDK 7.68, users can't close GADInterstitial, the close button is upper the safe area (user interaction is not permitted here) :
It's happening at least on iPhone 12 pro and iPhone 12 pro max.
What I tried :
GADInterstitial *interstitial = [[GADInterstitial alloc] initWithAdUnitID:#"xxx"];
GADRequest *request = [GADRequest request];
[interstitial loadRequest:request];
Hiding status bar let the user click on the close button. Ugly deprecated method, but it's working for now.
/// Tells the delegate that an interstitial will be presented.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
/// Tells the delegate an ad request failed.
- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
/// Tells the delegate the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}

Local Notifications - asking for permission again after initially rejected

When an app launches for the first time, it will ask the user for permission to send local notifications.
If the user denies the permission at launch, how do we ask for permission again in the future?
For example, is there a way that on a button click, I can request permission again?
Here is my code to initially ask for permission at launch:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// are you running on iOS8?
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
}
If for the first time you have disallowed local notification and on the second time use the blow mentioned code:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(currentUserNotificationSettings)]
{
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"app-settings:"]];
}
}
This will open your application settings where you can find the settings to enable/disable local notification and other settings.
Hope this helps!
You can not request the system prompt again. You can detect a situation in which the access was denied and show an alert yourself or show the button that will open settings app to make the process of changing the setting easier for the user.

Some time apple push notification not getting

I am developing an iOS application in iOS 8 .That has one module called message sending. This module developed with help of Apple push notification. My issue is some time we are not getting Push message to iOS device .The server successfully sent the message to APNS. Is it any APNS reliability issue?
i am using the following steps:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
APNs are not reliable, they are not 100% guaranteed to reach the client.
As of Apple's documentation, APNs are best effort, so many times they might not reach.
Via answer
Samraan is not wrong, Apple does not guarantee delivery, however it is pretty reliable in my experience. What I do notice from the code you have posted here is that you have an empty implementation for didReceiveRemoteNotification. When your app is active, and it receives a push notification, there is no system banner or alert shown. Instead the notification is delivered to the didReceiveRemoteNotification callback for your app to handle directly. If you have an empty definition of this function and you send a notification to that device, it may appear like the notification never arrived, but instead it was just hidden.
You could try adding a UIAlertView popup to this callback to find out if this is what is happening in your case.

Unexpected behaviour from registerUserNotificationSettings: and registerForRemoteNotifications

I am getting what I believe is an expected behaviour with Push Notifications in iOS 8 in terms of what kind of UIUserNotificationType I should received.
The test I have run is the following:
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication registerUserNotificationSettings:mySettings];
[sharedApplication registerForRemoteNotifications];
I get the expected UIApplicationDelegate calls and when I send a Push Notification, I receive an alert as expected. All fine until here.
Then, I unregister for remote notifications:
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
And I ran the same code as above but in this case, I do not call registerUserNotificationSettings:
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication registerForRemoteNotifications];
I again get all the expected UIApplicationDelegate calls but when I send the same Push Notification, I get the alert on my device and I believe that is not correct. I think I should not get any alert, sound or badge as the the documentation says:
If you want your app’s remote notifications to display alerts, play sounds, or perform other user-facing actions, you must call the registerUserNotificationSettings: method to request the types of notifications you want to use. If you do not call that method, the system delivers all remote notifications to your app silently.
To double check that, I have run just the second snippet on a different device:
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication registerForRemoteNotifications];
And in that case I did not get any alert as expected.
Is that the normal behaviour? I would expect that after unregistering, the preferences expressed in the next registration (no preferences in this case) would take precedence but it seems that when registerUserNotificationSettings: is not called, the previous settings are cached somewhere and used to decide what kind of notifications are delivered to the app.
In order to achieve the behavior you want, you should call to registerUserNotificationSettings with a setting configured with no type. This way you will override the cached settings. Like this:
UIUserNotificationType types = UIUserNotificationTypeNone;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication registerUserNotificationSettings:mySettings];
[sharedApplication registerForRemoteNotifications];
If you don't reconfigure the user notification settings, the cached value (that, as you have noticed, persists application reinstalls) will be used.

Push Notifications Stopped working iOS8 beta

So this just adds to the numerous issues i've had with the latest iOS8 beta update. My app was working fine using parse to send push notifications. It was working great on my phone and on the iOS7 simulators. However now I'm not receiving the notifications at all. Parse is saying my phone is still registered which it is. The console is giving me the error "registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later." Which is understandable however I believe my code solves that issue and until this update it was. I noticed as well when running the simulator that if my app is deleted from it and then I run the program again in the simulator. No dialog box is appearing asking if I want to accept push notifications, instead it is automatically setting it so they are accepted, it's doing it on my phone as well. As a side note this could potentially be a bug with the update as my other push notifications have been late/random today from other apps. However I would appreciate someone looking over the code to check please.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:252/255.0f green:230/255.0f blue:17.0/255.0f alpha:1.0]];
[[UIToolbar appearance] setBarTintColor:[UIColor colorWithRed:252/255.0f green:230/255.0f blue:17.0/255.0f alpha:1.0]];
[Parse setApplicationId:#"***"
clientKey:#"***"];
// Register for push notifications
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
#ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
return YES;
}
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:#"declineAction"]){
}
else if ([identifier isEqualToString:#"answerAction"]){
}
}
#endif
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
UPDATE: I created a new app in Parse and copied the application IDs etc into it (missing the certificates stage of setting it up as they won't change right?) And now even though the app says in settings that it is registered to receive notifications, again without asking) Parse says that it is not.
Don't know how or why? But the pushes have started working again. Haven't changed any of the settings from what is above so if anyone is looking for a solution to using parse this works apparently!
For iOS8 to receive push notification take a look on this :
Not able to set Interactive Push Notifications on iOS8
The push notification is working and appearing, but the interactive buttons aren't working.
I think there's something related to the category..
If any one found something please update.