gpt4 book ai didi

ios - applicationReceivedRemoteMessage 只在前台执行

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:00 24 4
gpt4 key购买 nike

我的项目使用 Firebase 通知作为其 APNs 服务,但我一直在使用 Firebase 控制台向我的设备发送通知作为测试,它们只在前台显示(通过控制台输出)。当应用程序处于后台或设备处于锁定屏幕时,设备不会收到任何通知。但是,当我备份打开应用程序时,控制台输出最终确实从 applicationReceivedRemoteMessage 方法到达。

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {

print("%@", remoteMessage.appData)
print("QQQQQ")
}

示例输出:

%@ [AnyHashable("notification"): {
body = Hi;
e = 1;
}, AnyHashable("from"): 492525072004, AnyHashable("collapse_key"): org.myApp]
QQQQQ

最佳答案

iOS 和数据消息存在问题。它说 here那个

On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection.

所以必须有一个变通办法。类似于我的东西:

发送 2 个推送通知:

1) Normal to wake the users phone / initiate while the app is in background using this code:

{
"to" : "/topics/yourTopicName",
"notification" : {
"priority" : "Normal",
"body" : "Notification Body like: Hey! There something new in the app!",
"title" : "Your App Title (for example)",
"sound" : "Default",
"icon" : "thisIsOptional"
}
}

2) Data notification that will trigger when user opens the app

{
"to" : "/topics/yourTopicName",
"data" : {
"yourData" : "1",
"someMoreOfYourData" : "This is somehow the only workaround I've come up with."
}
}

因此,在 - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage 方法下处理您的数据:

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"%@", remoteMessage.appData);
//
//*** ABOUT remoteMessage.appData ***//
// remoteMessage.appData is a Key:Value dictionary
// (data you sent with second/data notification)
// so it's up to you what will it be and how will the
// app respond when it comes to foreground.
}

我还会留下这段代码来触发应用程序内部的通知(创建本地通知),因为你可以用它来创建一个静音横幅,也许,这样即使应用程序进入前台,用户也会再次收到通知:

NSDictionary *userInfo = remoteMessage.appData;    
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = userInfo[@"yourBodyKey"];
localNotification.alertTitle = userInfo[@"yourTitleKey"];
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

它会在应用进入前台的那一秒触发通知。

关于ios - applicationReceivedRemoteMessage 只在前台执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com