如何打开从通知到我的应用程序的链接?

时间:2018-02-19 20:20:17

标签: android

我在收到通知时尝试让我的应用在浏览器中打开一个链接,但无法找到适用于前台和后台应用的方法。

通知看起来像这样

  "notification": {
    "title": "My app try",
    "body": "This is a notification, YAY!",
  },
  "data": {
    "code":"1003",
    "url":"https://www.thisishard.com"
  }

如果应用已关闭,我需要显示通知,当用户点击该应用时,请打开该应用,然后打开浏览器。

但是,如果应用程序已打开,我需要显示通知,并且只有在用户点击它时才打开浏览器。

现在它只是打开浏览器,如果应用程序处于打开状态则不会打开,如果应用程序关闭则只打开它。

我使用firebase云消息发送通知。

编辑:

我所做的是,在onMessageReceived()中,使用Intent打开Web浏览器

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String intentUri = remoteMessage.getData().get("url");

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentUri));
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(browserIntent);
    }

1 个答案:

答案 0 :(得分:0)

您必须像这样

创建自己的通知&ont listener方法
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse("http://www.google.com")); 
        PendingIntent pi = PendingIntent.getActivity(context, 0, notificationIntent, 0);
         // Resources r = getResources();
          Notification notification = new NotificationCompat.Builder(context)
                  .setTicker("yortext")
                  .setSmallIcon(android.R.drawable.ic_menu_report_image)
                  .setContentTitle("yortext")
                  .setContentText("sdsd")
                  .setContentIntent(pi)
                  .setAutoCancel(true)
                  .build();

          NotificationManager notificationManager2 =  (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
          notificationManager2.notify(0, notification);
  

Here is a good example

相关问题