Umgang mit Push-Benachrichtigungen, wenn App nicht ausgeführt wird (oder vollständig getötet wurde)
Ich poste diese Lösung, wie es für mich arbeitete.
Wechseln Sie zu Ihrer AppDelegate.m-Datei.
Schritt 1: schreiben Dieser Code in dieser Funktion:
-(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];
}
}
Schritt 2:
Insert Dieser vollständige Code:
-(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];
}
}
Dieser ganze Code funktioniert, ob App ist Aktiv, Inaktiv oder Totally Getötet. Es gibt Ihnen AlertView für Push-Nachrichten.
Der Code, den Sie nicht notification.The Code, den Sie nur noch im Zusammenhang haben drücken zeigt einen Alarm anzeigen, wenn die App aktiv ist. Was versuchst du zu machen ? –
Ich erhalte Push-Benachrichtigungen auf meinem IOS-Gerät. Ich möchte nur, wenn ich auf diese Push-Nachricht klicke, dann sollte es meine App öffnen und diese Nachricht für den Benutzer anzeigen. Wäre glücklich, wenn Sie ausführlich mit dem richtigen Code führen. @Teja Nandamuri –