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?
You do basically the same thing as a reaction to pressing the button, just use
UIApplication.sharedinstead of usingapplicationprovided byAppDelegate, so e.g. (Swift syntax):Moreover, I would recommend implementing your own custom
UserNotificationManager: