具有推送通知的深层链接-FCM-Android [英] Deep Link with Push Notification - FCM - Android
问题描述
我想要的内容:我想向用户发送推送通知.当用户点击该通知时,用户应导航至特定活动.
我的工作:我在Firebase控制台中创建了一个深层链接.我实现了 FirebaseInstanceIdService & FirebaseMessagingService .我能够捕获从Firebase控制台发送的Firebase消息.
问题是什么:我无法捕获在Firebase控制台中创建的动态链接.
我的代码如下.
MyFirebaseInstanceIDService.java
公共类MyFirebaseInstanceIDService扩展了FirebaseInstanceIdService {private final String TAG ="MyFirebaseInstanceID";@Overridepublic void onTokenRefresh(){字符串refreshedToken = FirebaseInstanceId.getInstance().getToken();Log.e(TAG,刷新令牌:" + refreshedToken);}}
MyFirebaseMessagingService.java
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService {私有的最终String TAG ="MyFbaseMessagingService";@Override公共无效onMessageReceived(RemoteMessage remoteMessage){字符串消息= remoteMessage.getNotification().getBody();Log.e(TAG,"\ nmessage:" +消息);sendNotification(message);}私人无效sendNotification(String message){Intent intent = new Intent(this,TestDeepLinkActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);PendingIntentendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder builder =新的NotificationCompat.Builder(this).setAutoCancel(true).setContentTitle("FCM测试").setContentText(消息).setSound(defaultSoundUri).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark).setContentIntent(pendingIntent);NotificationManager管理器=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);manager.notify(0,builder.build());}}
Firebase控制台映像
解决方案:
- 我必须在要推送通知的清单文件中的活动中添加意图过滤器.此通知将包含一些网址,这些网址在Android术语中称为深层链接".您可以在下面的链接中获得有关Deeplink的更多信息.
- 请参阅下面的代码,了解我如何将深度链接传递给NotificationManager.意向过滤器自动拦截并启动该特定活动.
MyFirebaseMessagingService.java
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService {@Override公共无效onMessageReceived(RemoteMessage remoteMessage){Map< String,String>数据= remoteMessage.getData();字符串标题= data.get("title");字符串message = data.get("message");字符串deepLink = data.get("deepLink");通知通知=新的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);如果(Build.VERSION.SDK_INT> = 26){NotificationChannel notificationChannel = new NotificationChannel("any_default_id","any_channel_name",NotificationManager.IMPORTANCE_HIGH);notificationChannel.setDescription(可以提供任何描述!");notificationManager.createNotificationChannel(notificationChannel);}Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);NotificationCompat.Builder notificationBuilder =新的NotificationCompat.Builder(上下文).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);PendingIntentendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_ONE_SHOT);notificationBuilder.setContentTitle(标题).setContentText(消息).setContentIntent(pendingIntent);notificationManager.notify(0,notificationBuilder.build());}}
AndroidManifest.xml
<代码><活动android:name =.mvp.view.activity.ActivityName"android:label ="@ string/title_activity_name"android:theme ="@ style/AppTheme.NoActionBar"><意图过滤器>< action android:name ="android.intent.action.VIEW"/>< category android:name ="android.intent.category.DEFAULT"/>< category android:name ="android.intent.category.BROWSABLE"/><数据android:host ="www.somedomain.com"android:path ="/about"android:scheme ="app"/></intent-filter><意图过滤器>< action android:name ="android.intent.action.VIEW"/>< category android:name ="android.intent.category.DEFAULT"/>< category android:name ="android.intent.category.BROWSABLE"/><数据android:host ="www.somedomain.com"android:path ="/contact"android:scheme ="app"/></intent-filter></activity>
其他:
-
如果您想在该活动中接收更多数据(即userId或loanId),则可以在从服务器(即后端或基于Web的仪表板)发送推送通知时将其传递给.您可以像下面这样.
{数据": {"userId":"65431214564651251456","deepLink":"www.somedomain.com/app","title":这是标题!","message":这是消息!"},至":"FCM令牌在这里"}
-
重要提示:JSON以下不起作用,仅供参考.文档中也没有提到这一点.因此,请照顾好它.正确的JSON在上方.
{至":"FCM令牌在这里",通知": {"Body":本周的版本现已发行.","title":"NewsMagazine.com","icon":新"},数据": {"title":这是标题!","message":这是消息!"}}
- 您可以在 MyFirebaseMessagingService 类的 onMessageReceived 方法中接收额外的数据(即userId或loanId),如下所示.
String userId = data.get("userId");intent.putExtra(Intent.EXTRA_TEXT,userId);
- 在该活动中,您可以像下面的onCreate方法中那样编写.
Intent intent = getIntent();if(intent!= null){字符串intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);如果(intentStringExtra!= null){userId = intentStringExtra;}}
What I want: I want to send push notification to users. When user tap on that notification, user should navigate to specific activity.
What I did: I created one deep link in Firebase console. I implemented FirebaseInstanceIdService & FirebaseMessagingService as well. I'm able to catch Firebase message which I sent from Firebase console.
What is the issue: I'm not able to catch the dynamic link what I have created in Firebase console.
My code is like below.
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 Console Image
Solution:
- I have to add intent-filter in an activity in manifest file to which I want to go, on-tapping of push notification. This notification will have some url which is being called deeplink in Android Terminology. You can refer below link for more about deeplink.
https://developer.android.com/training/app-links/deep-linking
I was using these two links as a deeplink: "www.somedomain.com/about" & "www.somedomain.com/app".
Please do not add http or https in intent-filter, they are not supported. Chekout this conversation for more clarification. I'm putting an image of that chat as well, If in case in future the link gets expire.
- Please refer below code for how I'm passing deeplink to NotificationManager. intent-filter aumatically intercept and launch that particular 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>
Extra:
If you want to receive some more data(i.e userId or loanId) in that activity, you can pass it to while sending push notification from your server(i.e back-end or web based dashboard). You can do like below.
{ "data": { "userId": "65431214564651251456", "deepLink": "www.somedomain.com/app", "title": "This is title!", "message": "This is message!" }, "to": "FCM token here" }
Important: Below JSON will not work, this is for reference only. This is also nowhere mentioned in documentation. So kindly take care of it. Correct JSON is above.
{
"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!"
}
}
- You can receive extra data(i.e userId or loanId) in method onMessageReceived of MyFirebaseMessagingService class like below.
String userId = data.get("userId");
intent.putExtra(Intent.EXTRA_TEXT, userId);
- And in that activity you can write like below in onCreate method.
Intent intent = getIntent();
if (intent != null) {
String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
if (intentStringExtra != null) {
userId = intentStringExtra;
}
}
这篇关于具有推送通知的深层链接-FCM-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!