iOS:应用程序打开时显示通知消息

时间:2014-11-13 14:34:44

标签: ios objective-c iphone xcode apple-push-notifications

我的应用程序中有textview和imageview,因此我可以显示通知文本和我想要显示的图像。我从通过通知发送的网址收到此图片。当我向我的应用程序发送通知并且应用程序未运行时,textview和imageview不会显示内容,但是当应用程序运行或应用程序状态处于后台时,内容会更新。

我想我必须在应用启动时配置didFinishLaunchingWithOptions功能,以便更新视图控制器的字段。 我怎样才能做到这一点?以及为什么didReceiveRemoteNotification函数在应用启动时不会更新视图,并且仅在两种状态(活动和背景)中更新视图?

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"New notification received!");

[self.window makeKeyAndVisible];

//Notification recieved & handle the aps
NSDictionary * notificationDict = [userInfo objectForKey:@"aps"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:notificationDict];
NSString * alertString = [notificationDict objectForKey:@"alert"];
NSString * serverUrl = [notificationDict objectForKey:@"acme"];

NSLog(@"Notification text is: %@", alertString);

[[NSUserDefaults standardUserDefaults] setObject: alertString forKey: @"alertMSG"];
[[NSUserDefaults standardUserDefaults] synchronize];

//handle the notification when the app is active, inactive or in background
if ( application.applicationState ==  UIApplicationStateActive )
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController2"];
    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
    application.applicationIconBadgeNumber = 0;
}
else if(application.applicationState == UIApplicationStateInactive)
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController2"];
    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
    application.applicationIconBadgeNumber = 0;
}
else //ApplicationState Background
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController2"];
    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
    application.applicationIconBadgeNumber = 0;
}

}

修改

我已经配置didFinishLaunchingWithOptions以在从通知启动应用程序时启动我的控制器,但除了在视图控制器和视图控制器之间签署了观察者这一事实之外,内容仍然没有更新应用代表。我错过了什么?

这是代码:

AppDelegate.m

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

if (launchOptions != nil)
        {               
            NSDictionary * dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
            if (dictionary != nil)
            {
                [self handleRemoteNotification:dictionary];
            }
        }
}

-(void)handleRemoteNotification:(NSDictionary*)payload{
    NSDictionary * notificationDict = [payload objectForKey:@"aps"]; //extract some information from payload
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:notificationDict];

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController2"];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;}

ViewController2.m:

 - (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

//Observer for remote notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView:)  name:@"refreshView" object: nil];

notifTextf.delegate = self;

}

//Update View when notification is received
-(void)refreshView:(NSNotification *) notification
{
// Do whatever you like to respond to text changes here.

NSLog(@"observer notification message text is: %@", [notification.object objectForKey:@"alert"]);
NSLog(@"observer notification message url is: %@", [notification.object objectForKey:@"acme"]);


[self.notifTextf setText: [notification.object objectForKey:@"alert"]];

//Download image from Url sended by notification and display it
NSString * imageurl = [notification.object objectForKey:@"acme"];
NSURL *url = [NSURL URLWithString:imageurl];
self.notifImg.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url]];

}

1 个答案:

答案 0 :(得分:1)

  

为什么didReceiveRemoteNotification函数不会更新视图   当应用程序启动并仅在两种状态下更新视图时(活动&   背景)?

如果推送通知到达时应用程序未运行,系统将启动应用程序并在启动选项字典中提供相应的信息。该应用不会调用application:didReceiveRemoteNotification:方法来处理该推送通知。相反,您对application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法的实现需要获取推送通知有效负载数据并做出相应的响应。

尽可能实现application:didReceiveRemoteNotification:fetchCompletionHandler:方法(iOS 7及更高版本)。

与仅在应用程序在前台运行时调用的application:didReceiveRemoteNotification:方法不同,当您的应用程序在前台或后台运行时,系统会调用application:didReceiveRemoteNotification:fetchCompletionHandler:方法。此外,如果您启用了远程通知后台模式(要支持此模式,请在应用的UIBackgroundModes文件中包含remote-notification值和Info.plist值),系统会启动您的应用(或者从暂停状态唤醒它并在推送通知到达时将其置于后台状态。但是,如果用户强行退出,系统不会自动启动您的应用。在这种情况下,用户必须重新启动您的应用程序或重新启动设备,然后系统才会再次尝试自动启动您的应用。

Continue reading...