[英]How to open UIUserNotificationSettings in any uiviewcontroller
通常,我在Appdelegate.m中設置UIUserNotificationSettings
- (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);
}
但是現在我想在UIViewcontroller中使用它,例如UIViewcontroller中有一個UIButton,當我單擊它時,它將調用此函數,有什么想法嗎?
試試這個:
- (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;
}
要么
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];
}
您所做的基本上與按下按鈕的反應相同,只是使用UIApplication.shared
而不是使用AppDelegate
提供的application
,因此例如(Swift語法):
UIApplication.shared.registerForRemoteNotifications()
此外,我建議您實現自己的自定義UserNotificationManager
:
class UserNotificationManager: NSObject, UNUserNotificationCenterDelegate { ... }
希望下面的代碼對您有所幫助,
創建一個名為UIUserNotifications的新類,並將以下代碼添加到.h文件中
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIUserNotificationsHandler : NSObject
+ (void)registerUserNotifications;
@end
並將以下代碼添加到.m文件。 然后從您的ViewController類調用該函數
#import "UIUserNotificationsHandler.h"
@implementation UIUserNotificationsHandler
+ (void)registerUserNotifications{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge
| UIUserNotificationTypeSound
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.