我想向用户发送推送通知,要求他们更新应用程序,当他们点击它时,我希望他们转到应用程序商店/Playstore。
如何在 Firebase 通知上添加 URL?使用键值对?
有人可以帮忙吗?
您可以将 URL 包含在通知的数据负载中。当用户点击通知时,通知单击事件会将其重定向到指定的 URL。例如,您的 JSON 负载可能如下所示:
{
"to": "<FCM_TOKEN>",
"notification": {
"title": "Update Available",
"body": "A new version of the app is available. Please update now."
},
"data": {
"url": "https://play.google.com/store/apps/details?id=com.yourapp"
}
}
在您的应用程序中,您需要处理通知单击事件并将用户重定向到 URL。根据您的应用程序是适用于 Android 还是 iOS,这可以采用不同的方式完成,但您说过您想同时为两者执行此操作。
因此,对于 Android,您需要创建一个扩展
FirebaseMessagingService
的服务并重写 onMessageReceived
方法。在此方法中,检查消息是否包含数据,提取 URL,然后使用 Intent
处理 URL。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// check if the message contains data
if (remoteMessage.getData().size() > 0) {
Map<String, String> data = remoteMessage.getData();
String url = data.get("url");
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), url);
}
}
private void sendNotification(String title, String messageBody, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
在您的
AndroidManifest.xml
: 中注册此服务
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
对于 iOS,您需要修改
AppDelegate.swift
来处理通知。确保应用程序配置为处理通知并在收到通知时打开 URL。
import UserNotifications
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let url = userInfo["url"] as? String {
if let url = URL(string: url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
completionHandler(UIBackgroundFetchResult.newData)
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let url = userInfo["url"] as? String {
if let url = URL(string: url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
completionHandler()
}
}