当用户单击推送通知时,我试图用一个URL打开浏览器,我在stackoverflow中搜索,然后找到了这个

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);
但这对我不起作用。
我正在寻找过去5天的解决方案,但找不到该解决方案。
这是我的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

// Intent notificationIntent = new Intent(this, HomeActivity.class);

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.example.android"));
startActivity(notificationIntent);


notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);

int notificationId = new Random().nextInt(60000);
Bitmap bitmap = getBitmapfromUrl(remoteMessage.getData().get("image-url"));

Intent likeIntent = new Intent(Intent.ACTION_VIEW);
likeIntent.putExtra(NOTIFICATION_ID_EXTRA,notificationId);
likeIntent.putExtra(IMAGE_URL_EXTRA,remoteMessage.getData().get("image-url"));
PendingIntent likePendingIntent = PendingIntent.getService(this,
notificationId+1,likeIntent, PendingIntent.FLAG_ONE_SHOT);


Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupChannels();
}

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setLargeIcon(bitmap)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("title"))
.setStyle(new NotificationCompat.BigPictureStyle()
.setSummaryText(remoteMessage.getData().get("message"))
.bigPicture(bitmap))/*Notification with Image*/
.setContentText(remoteMessage.getData().get("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.addAction(R.drawable.ic_favorite_true,
getString(R.string.notification_add_to_cart_button),likePendingIntent)
.setContentIntent(pendingIntent);

notificationManager.notify(notificationId, notificationBuilder.build());

}
寻找任何帮助..

最佳答案

到目前为止,您所做的任何尝试都是正确的。我已经在示例android应用上尝试过您的代码,

package com.example.notificationapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private static final String NOTIFICATION_CHANNEL_ID = "Notification Channel Name";
    private static final String NOTIFICATION_CHANNEL_NAME = "Notifications";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Intent notificationIntent = new Intent(this, HomeActivity.class);

        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.example.android"));
        startActivity(notificationIntent);

        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_ONE_SHOT);

        int notificationId = new Random().nextInt(60000);
//        Bitmap bitmap = getBitmapfromUrl(remoteMessage.getData().get("image-url"));

//        Intent likeIntent = new Intent(Intent.ACTION_VIEW);
//        likeIntent.putExtra(NOTIFICATION_ID_EXTRA,notificationId);
//        likeIntent.putExtra(IMAGE_URL_EXTRA, remoteMessage.getData().get("image-url"));
//        PendingIntent likePendingIntent = PendingIntent.getService(this,
//                notificationId+1,likeIntent, PendingIntent.FLAG_ONE_SHOT);


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                        //.setLargeIcon(bitmap)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("some random title")
//                        .setStyle(new NotificationCompat.BigPictureStyle()
//                                .setSummaryText("message from remote message")
//                                .bigPicture(bitmap))/*Notification with Image*/
                        .setContentText("Adding some text within content text")
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
//                        .addAction(R.drawable.ic_favorite_true,
//                                getString(R.string.notification_add_to_cart_button),likePendingIntent)
                        .setContentIntent(pendingIntent);

        notificationManager.notify(notificationId, notificationBuilder.build());
    }
}
这就是我所看到的
java - 如何在Android推送通知上点击外部浏览器中的URL?-LMLPHP
因此,我看到使用指定的URL打开浏览器。
我高度怀疑您的MyFirebaseMessagingService中的onMessageReceived方法没有被调用。请在onMessageReceived中添加一条日志消息,看看您是否正在被调用,如果还没有尝试过的话,这可能是下一步。

09-10 12:49
查看更多