How to open UIUserNotificationSettings in any uiviewcontroller

299 Views Asked by At

Usually, I set UIUserNotificationSettings in Appdelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   //some codes
   UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert
                                        | UIUserNotificationTypeBadge
                                        | UIUserNotificationTypeSound
                                                                         categories:nil];
  [application registerUserNotificationSettings:settings];
  [application registerForRemoteNotifications];

  return YES;
 }

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

 }

 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);
 }

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  NSLog(@"Receive remote notification : %@",userInfo);
 }

but now I want to use it in a UIViewcontroller, such as there's a UIButton in a UIViewcontroller, when I click it, it will call this function, any ideas?

3

There are 3 best solutions below

0
Milan Nosáľ On

You do basically the same thing as a reaction to pressing the button, just use UIApplication.shared instead of using application provided by AppDelegate, so e.g. (Swift syntax):

UIApplication.shared.registerForRemoteNotifications()

Moreover, I would recommend implementing your own custom UserNotificationManager :

class UserNotificationManager: NSObject, UNUserNotificationCenterDelegate { ... }
0
kalpesh satasiya On

Try this one:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
     NSLog(@"Receive remote notification : %@",userInfo);

     YourViewController *login=[storyboard instantiateViewControllerWithIdentifier:@"YourViewContollerIDName"];
     MainNavigationViewController *mainNavigation=[storyboard instantiateViewControllerWithIdentifier:@"MainNavigationViewController"];
     [mainNavigation setViewControllers:@[login] animated:NO];
     self.window.rootViewController=mainNavigation;
}

OR

Appdelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
     NSLog(@"Receive remote notification : %@",userInfo);


[[NSNotificationCenter defaultCenter]postNotificationName:@"Notification" object:self userInfo:userInfo];


}

YourViewContoller.m

-(void)viewWillAppear:(BOOL)animated{



    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveNotification:)
                                             name:@"Notification"
                                           object:nil];



}

- (void)receiveNotification:(NSNotification *) notification{


    if ([[notification name] isEqualToString:@"Notification"]) {
       //call your function
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
0
Jayachandra A On

I hope below code helps you,

create a new class called UIUserNotifications, and add the following code to the .h file

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UIUserNotificationsHandler : NSObject
+ (void)registerUserNotifications;
@end

and following code to the .m file. Then call that function from your ViewController class

#import "UIUserNotificationsHandler.h"

@implementation UIUserNotificationsHandler
+ (void)registerUserNotifications{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert
                                            | UIUserNotificationTypeBadge
                                            | UIUserNotificationTypeSound
                                            categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
@end