通常,我在Appdelegate.m中设置UIUserNotificationSettings
<a href="###">http://internal.com/</a>
但是现在我想在UIViewcontroller中使用它,比如在UIViewcontroller中有一个UIButton,当我点击它时,它会调用这个函数,有什么想法吗?
答案 0 :(得分:1)
试试这个:
- (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];
}
答案 1 :(得分:0)
基本上与按下按钮的反应基本相同,只需使用UIApplication.shared
而不是使用application
提供的AppDelegate
,例如, (Swift语法):
UIApplication.shared.registerForRemoteNotifications()
此外,我建议您实施自己的自定义UserNotificationManager
:
class UserNotificationManager: NSObject, UNUserNotificationCenterDelegate { ... }
答案 2 :(得分:0)
我希望以下代码可以帮助您,
创建一个名为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