如何在点击 android 推送通知时在外部浏览器中打开 url?
Posted
技术标签:
【中文标题】如何在点击 android 推送通知时在外部浏览器中打开 url?【英文标题】:How to open url in external browser on Tap on android push notifications? 【发布时间】:2020-08-05 22:20:23 【问题描述】:当用户单击推送通知时,我尝试使用 url 打开浏览器,我在 *** 中搜索并找到了这个
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());
寻求帮助..
【问题讨论】:
问题是它打开的是游戏市场而不是网络浏览器? 【参考方案1】:到目前为止,您所尝试的一切都是正确的。我已经在示例 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());
这是我看到的,
所以我看到浏览器正在使用指定的 url 打开。
我高度怀疑您在 MyFirebaseMessagingService 中的 onMessageReceived
方法没有被调用。请在 onMessageReceived 中添加一条日志消息,看看您是否被调用,如果您还没有尝试过,这可能是接下来要做的事情。
【讨论】:
【参考方案2】:我正在使用这个 sn-p,它运行良好:
override fun onMessageReceived(remoteMessage: RemoteMessage)
super.onMessageReceived(remoteMessage)
Log.d("TAG", "From: $remoteMessage.from")
// Check if message contains a data payload.
if (remoteMessage.data.isNotEmpty())
Log.i("TAG", "Message data payload: $remoteMessage.data")
if (/* Check if data needs to be processed by long running job */ true)
handleNow(remoteMessage)
// Check if message contains a notification payload.
remoteMessage.notification?.let
Log.d("TAG", "Message Notification Body: $it.body")
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
private fun handleNow(remoteMessage: RemoteMessage)
try
val mBuilder =
NotificationCompat.Builder(
applicationContext,
"notify_001"
)
var intent = Intent(Intent.ACTION_VIEW, Uri.parse(
"market://details?id=" + applicationContext.packageName))
intent.action = "Unique_id"
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent = PendingIntent.getActivity(
applicationContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val bigText =
NotificationCompat.BigTextStyle()
for (item in remoteMessage.data.keys)
Log.i("TAG", "Key:" + item + " " + remoteMessage.data[item])
if (remoteMessage.data["big_text"] != null)
bigText.bigText(remoteMessage.data["big_text"])
if (remoteMessage.data["big_content_title"] != null)
bigText.setBigContentTitle(remoteMessage.data["big_content_title"])
if (remoteMessage.data["summary_text"] != null)
bigText.setBigContentTitle(remoteMessage.data["summary_text"])
a
mBuilder.setContentIntent(pendingIntent)
mBuilder.setContentTitle(remoteMessage.notification?.title)
mBuilder.setContentText(remoteMessage.notification?.body)
mBuilder.priority = NotificationCompat.PRIORITY_MAX
mBuilder.setAutoCancel(true)
mBuilder.setContentIntent(pendingIntent)
val bitmap =
BitmapFactory.decodeResource(
applicationContext.getResources(),
R.drawable.chandmahame_logo
)
mBuilder.setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(bitmap)
mBuilder.setStyle(bigText)
val mNotificationManager: NotificationManager = applicationContext
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
val channelId = "1234"
val channel = NotificationChannel(
channelId, "YOUR_CHANNEL_ID",
NotificationManager.IMPORTANCE_HIGH
)
mNotificationManager.createNotificationChannel(channel)
mBuilder.setChannelId(channelId)
mNotificationManager.notify(
NOTIFICATION_ID,
mBuilder.build()
)
catch (e: Exception)
e.printStackTrace()
当然,将其用作 Java 代码并不是什么大问题。
【讨论】:
【参考方案3】:我在 tutorialpoint.com 中搜索并通过按钮显示此内容,也许可以提供帮助。
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
【讨论】:
以上是关于如何在点击 android 推送通知时在外部浏览器中打开 url?的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中点击 One Signal 推送通知时如何导航到特定页面?