【发布时间】:2020-11-24 16:12:16
【问题描述】:
我一直在尝试通过深度链接实现 fcm 通知。我已经关注了这篇文章。 This Post 我使用 Cloud Messaging Dashboard 发送通知,并将自定义数据作为“deepLink”键,它是值,但我无法检索任何 remoteData ,我尝试对其进行 log.d ,但没有出现任何内容。因此,每当我发送通知时,它都会出现但会导致主要活动,而不是所需的深层链接活动。请帮忙。
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Map<String, String> data = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String title = data.get("title");
String message = data.get("message");
String deepLink = data.get("deepLink");
Log.d(TAG,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("xyz01", "xyz",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.drawable.ic_stat_notifylogo)
.setPriority(android.app.Notification.PRIORITY_MAX)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_notifylogo));
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());
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void handleNow() {
//Log.d(TAG, "Short lived task is done.");
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
我想发送给用户的活动清单(/处理动态链接的地方)。
<activity android:name="c.testnotification.app.MainActivity" android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</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="https://somedomainxyz.com/?id="
android:scheme="https"/>
<data
android:host="https://somedomainxyz.com/?id="
android:scheme="http"/>
</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="somedomainxyz.com"
android:path="/?id="
android:scheme="app" />
</intent-filter>
</activity>
这就是我通过仪表板添加自定义数据的方式
我尝试通过其他方式发送,例如完整的深层链接 URL,而不是短链接、http、https 等。但仍然没有成功。
这种方法也足以管理这两种情况,即。当应用程序处于前台和后台时。
【问题讨论】:
-
请发布您发送的通知示例
-
请显示您的清单文件,其中您定义了要将用户发送到的活动。或者如果它不是太大,就发布整个清单
-
@Kubicko 我附上了截图。请检查。
-
@SarahKhan 我已在清单中添加了该类的 sn-p。请检查。
-
好的,那么在MainActivity中如何提取主机呢?您介意在您的 MainActivity 中发布代码吗?您在哪里接收通过深度链接发送的 url?或者您可以只发布整个 MainActivity 代码吗?
标签: android firebase firebase-cloud-messaging android-notifications