当应用程序未运行(或完全死亡)时处理推送通知
我发布此解决方案,因为它为我工作。
转到您的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];
}
}
这整个代码将工作,无论是应用程序有效,无效或完全死亡。它会为您提供推送消息的AlertView。
,你必须把你刚才notification.The代码是不相关的代码显示警报查看应用何时处于活动状态。你想做什么 ? –
我在我的IOS设备上收到推送通知。我只是想当我点击推送消息,而不是它应该打开我的应用程序,并显示该消息给用户多数民众赞成它。如果您使用正确的代码详细指导,会很高兴。 @Teja Nandamuri –