Ich versuche, eine lokale Benachrichtigung zu senden, um die Ausweisnummer auf meiner Registerkarte zu ändern, wenn eine Remote-Benachrichtigung empfangen wird. Wenn eine Benachrichtigung erhalten wird, wenn die App geöffnet ist, wird meine else-Anweisung unten perfekt ausgelöst. Wenn sich die App jedoch im Hintergrund befindet, scheinen meine ersten beiden if-Anweisungen niemals zu funktionieren.iOS/Obj-C - didReceiveRemoteNotification-Anweisungen werden nicht ausgelöst?
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
completionHandler(UIBackgroundFetchResultNewData);
}
else if (application.applicationState == UIApplicationStateBackground) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"application Background - notification has arrived when app was in background");
NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];
if([contentAvailable isEqualToString:@"1"]) {
NSLog(@"content-available is equal to 1");
completionHandler(UIBackgroundFetchResultNewData);
}
}
else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"application Active - notication has arrived while app was opened");
completionHandler(UIBackgroundFetchResultNewData);
}
}
OR: Ich habe auch versucht, den Code unten, und gleiches Problem ...
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"App notification received!");
// do stuff when app is active
}else{
static int i=1;
[UIApplication sharedApplication].applicationIconBadgeNumber = i++;
NSLog(@"App notification received background!");
}
}
THE FIX (für diejenigen mit Drupal): Fügen Sie Folgendes zu push_notifications.admin.inc hinzu (Drupal Push Notifications-Datei) **
$payload['content-available'] = 1;
Wenn die App im Hintergrund ist, wird die Benachrichtigung immer noch empfangen (zB sieht mein Benutzer die Popup-Blase mit der Nachricht - also content-available = 1 muss schon gesetzt sein, nein?), Aber die if-Anweisung noch scheint nicht zu schießen? – Brittany
überprüfen Sie bitte die Push-Notification Payload, der inhaltliche Schlüssel muss fehlen. Vor ein paar Tagen stand ich demselben Problem gegenüber. –
Interessant ... Ich benutze das Drupal Push Notifications-Modul, um dies zu erreichen. Nicht sicher, ob Sie sich auskennen, aber wie sollte ich prüfen, ob dieser Schlüssel in der Payload fehlt? https://www.drupal.org/project/push_notifications Alternativ - Gibt es eine Möglichkeit, content-available = 1 in meinem obj-c-Code zu setzen, wenn die Benachrichtigung empfangen wird? – Brittany