java - 点击推送通知后新 Activity 未打开

标签 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);

        }
    }
}

我面临的问题是,当我收到此应用程序的通知时,它是从登录 Activity 开始的,而不是转到 HomeActivity。 我知道,如果我想从通知中打开特定 Activity ,我需要在启动器 Activity 中指定 getIntent(),但我没有任何启动器 Activity 。 当我尝试在 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());

    }

}

最佳答案

当您想在单击通知时打开 Activity 时,请使用如下代码:

@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());

}

关于java - 点击推送通知后新 Activity 未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60468713/

相关文章:

java - 从示例中找不到 Selenium 类名

java - 我们可以在不更改 APP 版本的情况下更新 Google Play Store 上的 APK

android - OneSignal-Pushnotification 小图标在 Android 上不显示

javascript - 服务 worker 的推送事件未在多个选项卡中调用

node.js - 使用 NodeJS 对 Google 推送通知执行操作

java - groupsum问题

java - 不兼容的类型;找到 : interface java. util.List<java.lang.Object>,必需 : interface java. util.List<test.entity.Emp>

android - getFragmentById 返回 null

android - 如何/何时收集处理程序垃圾?

java - checkstyle getSQLTypeName