新活动未在推送通知单击时打开

时间:2020-02-29 19:14:18

标签: java android push-notification android-notifications

我有一个Home.java类,当我打开该应用程序以检查用户是否登录时,它首先运行。

public class Home extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();

        if (firebaseUser != null) {

            Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);

        }
    }
}

我面临的问题是,当我收到此应用程序的通知时,它是从LogIn活动而不是HomeActivity开始的。 我知道,如果要从通知中打开特定活动,则需要在启动器活动中指定getIntent(),但我没有任何启动器活动。 当我尝试在Home.java类中实现getIntent()时,由于未扩展Activity类,因此未导入该方法。 任何帮助!

NotificationService.java

public class NotificationService extends FirebaseMessagingService {

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

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

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, "Restaurants")
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setSound(uri);     //applying default notification sound to the notification


        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        int id = (int) System.currentTimeMillis();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("Restaurants", "demo", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        notificationManager.notify(id, notificationBuilder.build());

    }

}

1 个答案:

答案 0 :(得分:0)

当您要打开活动时,单击“通知”,然后使用如下代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

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

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, "Restaurants")
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setSound(uri);     //applying default notification sound to the notification

    //Create an intent to gom HomeActivity 
    Intent intent = new Intent(this, HomeActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    int id = (int) System.currentTimeMillis();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel("Restaurants", "demo", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    notificationManager.notify(id, notificationBuilder.build());

}