运行 收到 APN 时的功能
Run function when APN is received
我能够将 APNS 集成到我的应用程序中。现在我想在用户点击通知时或用户在使用应用程序时收到通知时处理通知。我使用下面的代码在收到通知时显示警告对话框:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// display the userInfo
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// Find the presented VC...
var presentedVC = self.window?.rootViewController
while (presentedVC!.presentedViewController != nil) {
presentedVC = presentedVC!.presentedViewController
}
presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)
// call the completion handler
// -- pass in NoData, since no new data was fetched from the server.
completionHandler(UIBackgroundFetchResult.NoData)
}
}
是否可以 运行 我的 MainViewController 中的函数而不是显示对话框?我试着调用我的函数 MainViewController.refreshData()
但它总是给我一个 nil
.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// display the userInfo
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
//Your Function call when you click Ok Button.
self.YourFunc()
}))
// Find the presented VC...
var presentedVC = self.window?.rootViewController
while (presentedVC!.presentedViewController != nil) {
presentedVC = presentedVC!.presentedViewController
}
presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)
// call the completion handler
// -- pass in NoData, since no new data was fetched from the server.
completionHandler(UIBackgroundFetchResult.NoData)
}
}
是的,可以通过发布和添加观察员来实现。
使用 NSNotificationCenter 制作。
我在Objective C代码中发帖,尝试转换为Swift。
@implementation AppDelegate
static void displayStatusChanged(CFNotificationCenterRef center, void *observer,
CFStringRef name, const void *object,
CFDictionaryRef userInfo) {
if (name == CFSTR("com.apple.springboard.lockcomplete")) {
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:@"kDisplayStatusLocked"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged,
CFSTR("com.apple.springboard.lockcomplete"), NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
return YES;
}
然后将此代码粘贴到 didRecieveRemoteNotification
[[NSNotificationCenter defaultCenter]postNotificationName:@"TestNotification" object:self];
并且在您的 MainViewController
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"]) {
NSLog (@"Successfully received the test notification!");
//perform your operations
[self refreshData];
}
}
我能够将 APNS 集成到我的应用程序中。现在我想在用户点击通知时或用户在使用应用程序时收到通知时处理通知。我使用下面的代码在收到通知时显示警告对话框:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// display the userInfo
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// Find the presented VC...
var presentedVC = self.window?.rootViewController
while (presentedVC!.presentedViewController != nil) {
presentedVC = presentedVC!.presentedViewController
}
presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)
// call the completion handler
// -- pass in NoData, since no new data was fetched from the server.
completionHandler(UIBackgroundFetchResult.NoData)
}
}
是否可以 运行 我的 MainViewController 中的函数而不是显示对话框?我试着调用我的函数 MainViewController.refreshData()
但它总是给我一个 nil
.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// display the userInfo
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
//Your Function call when you click Ok Button.
self.YourFunc()
}))
// Find the presented VC...
var presentedVC = self.window?.rootViewController
while (presentedVC!.presentedViewController != nil) {
presentedVC = presentedVC!.presentedViewController
}
presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)
// call the completion handler
// -- pass in NoData, since no new data was fetched from the server.
completionHandler(UIBackgroundFetchResult.NoData)
}
}
是的,可以通过发布和添加观察员来实现。
使用 NSNotificationCenter 制作。
我在Objective C代码中发帖,尝试转换为Swift。
@implementation AppDelegate
static void displayStatusChanged(CFNotificationCenterRef center, void *observer,
CFStringRef name, const void *object,
CFDictionaryRef userInfo) {
if (name == CFSTR("com.apple.springboard.lockcomplete")) {
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:@"kDisplayStatusLocked"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged,
CFSTR("com.apple.springboard.lockcomplete"), NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
return YES;
}
然后将此代码粘贴到 didRecieveRemoteNotification
[[NSNotificationCenter defaultCenter]postNotificationName:@"TestNotification" object:self];
并且在您的 MainViewController
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"]) {
NSLog (@"Successfully received the test notification!");
//perform your operations
[self refreshData];
}
}