具有推送通知的深层链接-FCM-Android

Mau*_*dia 9 deep-linking firebase firebase-cloud-messaging firebase-dynamic-links android-push-notification

我想要的是:我想向用户发送推送通知。当用户点击该通知时,用户应导航到特定活动。

我做了什么:我在Firebase控制台中创建了一个深层链接。我也实现了FirebaseInstanceIdServiceFirebaseMessagingService。我能够捕获从Firebase控制台发送的Firebase消息。

问题是什么:我无法捕获在Firebase控制台中创建的动态链接。

我的代码如下。

MyFirebaseInstanceIDService.java

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private final String TAG = "MyFirebaseInstanceID";

    @Override
    public void onTokenRefresh() {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Log.e(TAG, "Refreshed token: " + refreshedToken);
    }
}
Run Code Online (Sandbox Code Playgroud)

MyFirebaseMessagingService.java

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private final String TAG = "MyFbaseMessagingService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String message = remoteMessage.getNotification().getBody();

        Log.e(TAG, "\nmessage: " + message);

        sendNotification(message);
    }

    private void sendNotification(String message) {

        Intent intent = new Intent(this, TestDeepLinkActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("FCM Test")
                .setContentText(message)
                .setSound(defaultSoundUri)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0, builder.build());
    }
}
Run Code Online (Sandbox Code Playgroud)

Firebase控制台映像

Firebase控制台映像

Mau*_*dia 5

解:

  • 我必须在要推送的清单文件中的活动中添加意图过滤器。该通知将包含一些在Android术语中称为Deeplink的网址。您可以参考下面的链接以获取有关Deeplink的更多信息。

https://developer.android.com/training/app-links/deep-linking

  • 我将这两个链接用作深层链接:“ www.somedomain.com/about”和“ www.somedomain.com/app”。

  • 请不要在意图过滤器中添加httphttps,因为它们不受支持。仔细讨论对话,以进一步澄清。我还要添加该聊天的图像,以防万一将来链接失效。

在此处输入图片说明

  • 请参阅以下代码,了解我如何将深层链接传递给NotificationManager。意向过滤器自动拦截并启动该特定活动。

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> data = remoteMessage.getData();

        String title = data.get("title");
        String message = data.get("message");
        String deepLink = data.get("deepLink");

        Notification notification = new Notification();
        notification.setTitle(title);
        notification.setMessage(message);
        notification.setDeepLink(deepLink);

        sendNotification(this, title, message, deepLink);
    }

    public static void sendNotification(Context context, String title, String message, String deepLink) {

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel notificationChannel = new NotificationChannel("any_default_id", "any_channel_name",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription("Any description can be given!");
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(android.app.Notification.PRIORITY_MAX)
                .setDefaults(android.app.Notification.DEFAULT_ALL)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));

        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(deepLink));
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        notificationBuilder
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent);

        notificationManager.notify(0, notificationBuilder.build());
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml

<activity
    android:name=".mvp.view.activity.ActivityName"
    android:label="@string/title_activity_name"
    android:theme="@style/AppTheme.NoActionBar">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="www.somedomain.com"
            android:path="/about"
            android:scheme="app" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="www.somedomain.com"
            android:path="/contact"
            android:scheme="app" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

额外:

  • 如果您想在该活动中接收更多数据(即userId或loanId),则可以在从服务器(即后端或基于Web的仪表板)发送推送通知时将其传递给。您可以像下面这样。

    {
     "data": {
     "userId": "65431214564651251456",
     "deepLink": "www.somedomain.com/app",
     "title": "This is title!",
     "message": "This is message!"
     },
    "to": "FCM token here"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 重要提示:JSON以下无法使用,仅供参考。文档中也没有提到这一点。因此,请照顾好它。上面是正确的JSON。

{
    "to": "FCM Token here",
        "notification": {
            "Body": "This week’s edition is now available.",
            "title": "NewsMagazine.com",
            "icon": "new"
        },
        "data": {
            "title": "This is title!",
            "message": "This is message!"
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 您可以在方法获得额外的数据(即用户id或的LoanID)onMessageReceivedMyFirebaseMessagingService类像下面。
{
 "data": {
 "userId": "65431214564651251456",
 "deepLink": "www.somedomain.com/app",
 "title": "This is title!",
 "message": "This is message!"
 },
"to": "FCM token here"
}
Run Code Online (Sandbox Code Playgroud)
  • 在该活动中,您可以像下面的onCreate方法中那样编写。
{
    "to": "FCM Token here",
        "notification": {
            "Body": "This week’s edition is now available.",
            "title": "NewsMagazine.com",
            "icon": "new"
        },
        "data": {
            "title": "This is title!",
            "message": "This is message!"
    }
}
Run Code Online (Sandbox Code Playgroud)

  • onMessageReceived() 仅在应用程序打开时执行。因此,在其他情况下(我认为大约 90% 的时间!),推送通知不适用于深度链接。 (3认同)
  • @PayamRoozbahaniFard 那是当您发送“通知”消息或“通知+数据”消息时。如果'数据消息发送出去(由你处理),则触发onMessageReceived(无论前台还是后台) (2认同)
  • @MaulikDodia是的,如果您的服务器仅在fcm有效负载中发送“数据”(而不是“通知”),那么每次都会调用onMessageReceived。参考[此处](https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages) (2认同)