具有推送通知的深度 Link - FCM - Android
Deep Link with Push Notification - FCM - Android
我想要什么:我想向用户发送推送通知。当用户点击该通知时,用户应导航至特定的 activity.
我做了什么: 我在 Firebase 控制台中创建了一个深度 link。我也实施了 FirebaseInstanceIdService & FirebaseMessagingService。我能够捕获从 Firebase 控制台发送的 Firebase 消息。
问题是什么: 我无法捕获我在 Firebase 控制台中创建的动态 link。
我的代码如下。
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);
}
}
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());
}
}
Firebase 控制台图像
解法:
- 我必须在清单文件中的 activity 中添加 intent-filter,我想点击推送通知。此通知将包含一些 url,在 Android 术语中称为 deeplink。你可以参考下面link了解更多关于deeplink.
https://developer.android.com/training/app-links/deep-linking
我用这两个 link 作为深度 link:"www.somedomain.com/about" & "www.somedomain.com/app".
请不要在 intent-filter 中添加 http 或 https,它们不受支持。 Chekout this 对话以获得更多说明。我也放了那个聊天的图片,如果将来 link 过期了。
- 请参考下面的代码,了解我如何将 deeplink 传递给 NotificationManager。 intent-filter 自动拦截并启动特定的 activity.
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());
}
}
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>
额外:
如果您想在 activity 中接收更多数据(即 userId 或 loanId),您可以在从您的服务器(即后端或基于 Web 的仪表板)。你可以像下面那样做。
{
"data": {
"userId": "65431214564651251456",
"deepLink": "www.somedomain.com/app",
"title": "This is title!",
"message": "This is message!"
},
"to": "FCM token here"
}
重要提示:低于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!"
}
}
- 您可以在 MyFirebaseMessagingService class 的 onMessageReceived 方法中接收额外数据(即 userId 或 loanId),如下所示。
String userId = data.get("userId");
intent.putExtra(Intent.EXTRA_TEXT, userId);
- 然后 activity 你可以在 onCreate 方法中像下面这样写。
Intent intent = getIntent();
if (intent != null) {
String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
if (intentStringExtra != null) {
userId = intentStringExtra;
}
}
我想要什么:我想向用户发送推送通知。当用户点击该通知时,用户应导航至特定的 activity.
我做了什么: 我在 Firebase 控制台中创建了一个深度 link。我也实施了 FirebaseInstanceIdService & FirebaseMessagingService。我能够捕获从 Firebase 控制台发送的 Firebase 消息。
问题是什么: 我无法捕获我在 Firebase 控制台中创建的动态 link。
我的代码如下。
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);
}
}
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());
}
}
Firebase 控制台图像
解法:
- 我必须在清单文件中的 activity 中添加 intent-filter,我想点击推送通知。此通知将包含一些 url,在 Android 术语中称为 deeplink。你可以参考下面link了解更多关于deeplink.
https://developer.android.com/training/app-links/deep-linking
我用这两个 link 作为深度 link:"www.somedomain.com/about" & "www.somedomain.com/app".
请不要在 intent-filter 中添加 http 或 https,它们不受支持。 Chekout this 对话以获得更多说明。我也放了那个聊天的图片,如果将来 link 过期了。
- 请参考下面的代码,了解我如何将 deeplink 传递给 NotificationManager。 intent-filter 自动拦截并启动特定的 activity.
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());
}
}
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>
额外:
如果您想在 activity 中接收更多数据(即 userId 或 loanId),您可以在从您的服务器(即后端或基于 Web 的仪表板)。你可以像下面那样做。
{ "data": { "userId": "65431214564651251456", "deepLink": "www.somedomain.com/app", "title": "This is title!", "message": "This is message!" }, "to": "FCM token here" }
重要提示:低于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!"
}
}
- 您可以在 MyFirebaseMessagingService class 的 onMessageReceived 方法中接收额外数据(即 userId 或 loanId),如下所示。
String userId = data.get("userId");
intent.putExtra(Intent.EXTRA_TEXT, userId);
- 然后 activity 你可以在 onCreate 方法中像下面这样写。
Intent intent = getIntent();
if (intent != null) {
String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
if (intentStringExtra != null) {
userId = intentStringExtra;
}
}