推送通知未打开内置的外部链接

时间:2018-08-16 14:39:15

标签: android firebase firebase-cloud-messaging

我使用完全相同的方法从通知中打开内容意图,但实际上是在FCM推送通知中打开我的应用程序。

我的代码:

public class ddkeysFirebaseMsgService extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    String deviceToken = s;
    saveString("deviceToken", deviceToken);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent downloadLink = new Intent(Intent.ACTION_VIEW);
    downloadLink.setData(Uri.parse("http://www.google.com"));

    PendingIntent opendwdlink = PendingIntent.getActivity(getApplicationContext(), 0, downloadLink, 0);

    String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
    String body = remoteMessage.getNotification().getBody();

    cloudNotificationManager.getDkInstance(getApplicationContext())
            .cloudNotification(title, body, opendwdlink);
}

}  

我的通知生成器在这里,如果这里有任何错误,请纠正我

public class cloudNotificationManager {

private static cloudNotificationManager dkInstance;
private final Context cloudcontext;

private cloudNotificationManager(Context context) {
    cloudcontext = context;
}

public static synchronized cloudNotificationManager getDkInstance(Context context) {
    if (dkInstance == null) {
        dkInstance = new cloudNotificationManager(context);
    }
    return dkInstance;
}

public void cloudNotification(String title, String body, PendingIntent dwdlink) {

    NotificationCompat.Builder cBuilder = new NotificationCompat.Builder(cloudcontext, ddKeys_init.CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true);
    if (dwdlink != null) {
        cBuilder.setContentIntent(dwdlink);
    }


    NotificationManager notificationManager = (NotificationManager) cloudcontext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (notificationManager != null) {
        notificationManager.notify(111, cBuilder.build());
    }
}


}

我已经按照其他帖子中显示的确切方法进行了操作,但是没有用。

我发现了我的问题。

问题还有其他。如果提供的链接位于我的APP内(打开的应用程序,任何活动),则将打开,但如果我没有打开我的应用程序(如在另一个应用程序或主屏幕中),则会打开该应用程序。

我仍然不知道该如何克服

2 个答案:

答案 0 :(得分:0)

在将ACTION设置为Intent之前,必须检查URL是否正确。检查以下代码,了解如何检查URL是否适合ACTION

public Intent getLinkIntent(String url)
{
      Intent intent = null;
      try {
            String patter = "^(http|https|ftp)://.*$";
            if (url.matches(patter)) {
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            }else{
                String newUrl = "https://" + url;
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(newUrl));
            }
      }catch (Exception e){
            e.printStackTrace();
      }

      return intent;
}

答案 1 :(得分:0)

我了解到,当从Firebase cloudmessaging服务发送通知时,如果该应用程序在后台运行,则会首先打开您的应用程序,仅当您的应用程序在前台运行时,您发送的带有通知的数据才会收到。 为了克服这种行为,我使用了 REST CLIENT API 发送数据消息。无论处于前台还是后台,这里的数据都将在设备上接收。 [据我所知,当强制关闭应用程序时,不是这样]

无论如何,我建议不要使用REST CLIENT API以外的Firebase云消息传递,因为如果该应用程序安装在该设备上,则无论它是否被强制关闭,并且其中还有更多设置(如稍后发送选项),它都可以传递消息也一样

相关问题