java - 如何在外部浏览器中打开 url 点击 android 推送通知?

标签 java push-notification firebase-cloud-messaging android-notifications

当用户点击推送通知时,我试图用一个 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());
    }
}
这是我看到的
enter image description here
所以我看到浏览器正在使用指定的 url 打开。
我非常怀疑你的 onMessageReceived不调用 MyFirebaseMessagingService 中的方法。请在 onMessageReceived 中添加一条日志消息,看看您是否被调用,如果您还没有尝试过,这可能是接下来要做的事情。

关于java - 如何在外部浏览器中打开 url 点击 android 推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63274316/

相关文章:

java - Jersey 测试框架: Test Container is null

android - 无法解析 : com. google.firebase :firebase-messaging:16. 0.5

java - MySQl 和 autoReconnect=true 仍然给我异常

java - 使用聚合 Arraylist 覆盖类中的 equals 方法

仅通过反射调用 Java 方法。注释?

ios - 当 APNS token 失效时,是否必须重复剩余的推送通知?

android - Firebase无法检测到我的Flutter应用,并且无法发送推送通知

javascript - 推送订阅错误未触发捕获

ios - 仅使用客户端代码发送 Firebase 云消息

android - 无法使用正确的 google-services.json 文件在 Android 上接收 FCM 推送通知