我想要什么:我想向用户发送推送通知。当用户点击该通知时,用户应导航到特定活动。
我做了什么:我在 Firebase 控制台中创建了一个深层链接。我也实现了FirebaseInstanceIdService和FirebaseMessagingService。我能够捕获从 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);
}
}
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 控制台映像