我使用此代码(请参阅下面的#我使用的代码(,当点击燃烧通知时,可以打开特定活动。我希望"燃烧通知"打开浏览器并转到该特定链接。
我在发送通知时使用Firebase控制台。我想使用此自定义数据。
Key: url
Value: www.randomurl.com
请参阅下面的代码,并帮助我在浏览器中实现打开URL而不是打开我的应用程序。
#我使用的代码。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
String message = remoteMessage.getData().get("message");
String imageUri = remoteMessage.getData().get("image");
String TrueOrFalse = remoteMessage.getData().get("JournalActivity");
//To get a Bitmap image from the URL received
bitmap = getBitmapfromUrl(imageUri);
sendNotification(message, bitmap, TrueOrFalse);
}
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("JournalActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.app_logo_new)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
谢谢!
在您的pendingIntent中,通过此意图。
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
首先,您需要将URL传递到您的sendnotification方法中。为此,请在字符串消息下添加以下行:
String url = remoteMessage.getData().get("url");
然后,将URL传递到您的发送名称方法。将URL添加到OnMessageCeived的最后一行:
sendNotification(message, bitmap, TrueOrFalse, url);
接下来,接受您的发送指导方法中的URL。这样的方法将URL添加到这样的标题:
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse, String url)
最后,将其添加到您的sendnotification中:
intent.setData(Uri.parse(url));
这将导致通知单击时在浏览器中打开您的链接。