Not Receiving Push Notifications Using Firebase und Objective C
Ich versuche, @ zu verwendFirebase
für Push-Benachrichtigung. Ich habe das @ erfolgreich installieFirebase
UndFirebase messaging
viaCocoapods
.
Ich habe den folgenden Code verwendet
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshCallback:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
UIUserNotificationType allNotificationsType = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationsType categories:nil];
[application registerUserNotificationSettings:settings];
[application isRegisteredForRemoteNotifications];
return YES;
}
-(void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
NSLog(@"Disconnect from FCM");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self connectToFirebase];
}
- (void) application:(UIApplication *) application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler{
// Pring The Message Id
NSLog(@"Mesage Id : %@",userInfo[@"gcm.message_id"]);
// Print Full Message
NSLog(@"%@",userInfo);
}
#pragma mark -- Custom Firebase code
- (void)tokenRefreshCallback:(NSNotification *) notification{
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"IstanceID token: %@", refreshedToken);
// Connect To FCM since connection may have failed when attempt before having a token
[self connectToFirebase];
}
-(void) connectToFirebase{
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error)
{
if ( error != nil)
{
NSLog(@"Unable to Connect To FCM. %@",error);
}
else
{
NSLog((@"Connected To FCM"));
}
}];
}
Wenn ich den obigen Code mit meinem iPhone ausführe, das mit @ verbunden iXcode
Ich habe folgende Probleme, wenn ich eine Nachricht von der Firebase-Konsole sende.
1). Wenn sich die App im Vordergrund befindet (aktiv), zeigt Log die folgende Meldung an:
Mesage Id : (null){
CustommData = "First Message";
"collapse_key" = "abcd.iospush.FireBasePush";
from = 555551391562;
notification = {
badge = 4;
body = "Test Message";
e = 1;
sound = default;
sound2 = default;
};
}
BeachtenMessage Id
istnull
.
2). Mein Telefon zeigt keine Benachrichtigung inNotification Center ob App im Vordergrund, Hintergrund oder geschlossen ist
Ich möchte, dass der Benutzer Push-Benachrichtigungen erhält, wenn sich die App im Hintergrund, im Vordergrund oder im Status "Geschlossen" befindet.