如何解决未打开通知单击的活动的问题

时间:2019-01-08 00:23:22

标签: android notifications firebase-cloud-messaging

我希望在单击通知时,该应用程序将打开Facebook页面。如果该应用已打开,并且我单击了通知,那么它将起作用。问题是当应用程序关闭时,我收到了通知,但是当我单击通知时,MainActivity打开了,而不是Facebook页面。

我试图寻找答案,但是没有任何作用

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "channel";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("Description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
        notificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
    Uri uri = Uri.parse(remoteMessage.getData().get("link"));
    Log.d("TAG",remoteMessage.getData().get("link"));
    try {
        ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);
        if (applicationInfo.enabled)
            uri = Uri.parse("fb://facewebmodal/f?href=" + remoteMessage.getData().get("link"));
        intent = new Intent(Intent.ACTION_VIEW, uri);

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.logo).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
            .setContentTitle(remoteMessage.getNotification().getTitle()).setContentText(remoteMessage.getNotification().getBody()).setContentInfo("Info");
    notificationManager.notify(0, builder.build());
}

}

清单:

<service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

我希望当我在关闭应用程序时单击通知时,会得到与打开应用程序时相同的结果,但是当打开应用程序时,facebook页面会打开,而当关闭应用程序时,只会打开主要活动。

0 个答案:

没有答案