如何在Android Studio后台打开应用程序时从Android通知打开URI



我有一个从通知中打开URI的方法,但它只有在应用程序处于前台时才有效。即使应用程序在后台,我也可以在代码中添加什么来打开通知URI?此方法在class PushNotificationService extends FirebaseMessagingService中调用

void openAnotherApp(@NonNull RemoteMessage remoteMessage, Context context, String title, String body, String commingUrl) {
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(commingUrl));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,CHANNEL_ID)
.setSmallIcon(R.drawable.notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}

在扩展FirebaseMessagingService类的MyFirebaseMessagingService类中使用此方法

private void sendNotification(String messageTitle, String messageBody,String key,String value) {
//'MainActivity' is the target activity. When notification will be clicked, 'MainActivity' will be triggered
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (value!= null) {
intent.putExtra(key, value);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
messageTitle,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

并在onMessageReceived方法中的同一类中调用它,并作为参数为其提供

String title = "";
String message = "";
String dataKey = remoteMessage.getData().get("openApp");
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
try {
title = remoteMessage.getNotification().getTitle();
} catch (Exception e) {
e.printStackTrace();
}
try {
message = remoteMessage.getNotification().getBody();
} catch (Exception e) {
e.printStackTrace();
}
}
sendNotification(title, message,"openApp",dataKey);

最后在你的主要活动上使用这个代码

Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.containsKey("openApp")) {
String url = bundle.getString("openApp");
System.out.println(url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

}

现在,如果你从Firebase发送一个类似链接的数据;打开应用程序";当你点击它时出现的通知它将打开主活动,然后它将打开链接。

最新更新