在点击Firebase通知时,在浏览器中打开网址而不是活动 - Android

时间:2018-02-11 18:51:13

标签: android firebase notifications firebase-cloud-messaging

我使用此代码(请参阅下面的#我使用的代码)在点击Firebase通知时打开特定活动。我希望Firebase通知打开浏览器并转到该特定链接,而不是打开特定活动。

我在发送通知时使用Firebase控制台。我想使用这个自定义数据。

Key: url
Value: www.randomurl.com

请参阅下面的代码并帮助我在浏览器中实现打开网址,而不是打开我的应用程序。

#我使用的代码。

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;

    }
}

}

谢谢!

2 个答案:

答案 0 :(得分:1)

在pendingIntent中,传递此意图。

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

答案 1 :(得分:0)

首先,您需要将url传递给sendNotification方法。为此,请在字符串消息:

下的OnMessageReceived中添加以下行
String url = remoteMessage.getData().get("url");

然后,将url传递给sendNotification方法。将url添加到onMessageReceived的最后一行:

sendNotification(message, bitmap, TrueOrFalse, url);

接下来,接受sendNotification方法中的url。将url添加到方法标题中,如下所示:

private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse, String url)

最后,在sendNotification的意图下添加:

intent.setData(Uri.parse(url));

这会导致通知在点击时在浏览器中打开您的链接。