gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:30:46 25 4
gpt4 key购买 nike

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

我想我必须在应用程序启动时配置 didFinishLaunchingWithOptions 函数才能更新 View Controller 的字段。我怎样才能做到这一点?为什么 didReceiveRemoteNotification 函数在应用启动时不更新 View ,并且仅在两种状态(事件和后台)下更新 View ?

- (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以在从通知启动应用程序时启动我的 Controller ,但除了我在 View Controller 和 View Controller 之间签署了观察者这一事实之外,内容仍然没有更新应用程序委托(delegate)。我缺少什么?

这是代码:

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]];

最佳答案

why the didReceiveRemoteNotification function doesnt update the view when the app starts and updates the view only in two states (active & background)?

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

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

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

Continue reading...

关于iOS:应用程序打开时显示通知消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com