我可以将推送通知发送到我的IOS设备。但是,当我点击该通知时,它只会打开应用程序。应用内没有显示任何消息。
我使用的代码:
if (application.applicationState == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"Show";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
[alertView release];
} else {
//Do stuff that you would do if the application was not active
}
但无法在上述代码的帮助下显示我的消息。以上代码仅在我的应用程序处于打开状态且处于前台状态时才起作用,而不仅仅显示此警报。
请帮忙。
感谢。
答案 0 :(得分:3)
当应用程序被完全杀死时获取通知代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
//opened from a push notification when the app is closed
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
//write you push handle code here
}
}
}
有关详细信息,请访问以下链接:Handling Push Notifications when App is Terminated
答案 1 :(得分:1)
来自苹果文档
“当您的应用必须启动时 收到通知,UIKit包括 UIApplicationLaunchOptionsLocalNotificationKey或 启动时UIApplicationLaunchOptionsRemoteNotificationKey键 选项字典传递给您的应用程序委托 应用程序:willFinishLaunchingWithOptions:和 application:didFinishLaunchingWithOptions:方法。存在 这些键让您知道有通知数据等待 处理并让您有机会配置您的应用程序的界面 适当。您无需在这些中处理通知 但方法。应用程序运行后,UIKit会调用其他方法 您的应用代表,例如 application:didReceiveLocalNotification:方法,给你一个 处理通知数据的机会。调用哪些方法 取决于您实施的方法和用户 与消息的系统UI交互。“
因此,请检查您的应用是否因为通知而启动,如果是,则显示对话框。
答案 2 :(得分:1)
我认为有一个简单的解决方案。
答案 3 :(得分:1)
在应用未运行(或完全被杀)时处理推送通知
我发布此解决方案,因为它对我有用。
转到AppDelegate.m文件。
第1步: 将此代码写在此函数中:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
第2步:
插入此完整代码:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo);
/**
* Dump your code here according to your requirement after receiving push
*/
if (application.applicationState == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateInactive){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
无论app是Active,InActive还是Totally Killed,这整个代码都能正常运行。它会为推送消息提供AlertView。
答案 4 :(得分:-2)
处理这类事情的最佳方法是在APN中使用深层链接。这样您就可以嵌入数据,然后可以在您的应用中处理这些数据并将用户定向到特定事件。
否则,您只能使用应用代理中的ApplicationDidEnterForeground
方法。只需将您的alertView代码放在那里,并随时将您的应用程序带入将要运行的前台。