我有一个类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/