Я пытаюсь открыть фрагмент с помощью push-уведомления, которое запускается в firebase. Это не работает. Вот MessageService.java
private void showNotification(String title, String body) {
String NOTIFICATION_CHANNEL_ID = "plan.services.test";
Intent openMain = new Intent(this, MainActivity.class);
openMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openMain.putExtra("checkNotification", "openOrder");
Log.d(TAG, "order string in msg service:"+openMain.getStringExtra("checkNotification"));
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent orderIntent = PendingIntent.getActivity(OrderMsgService.this, 1, openMain, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "order intent:"+ orderIntent);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
currentapiVersion = R.drawable.icon_notification;
} else{
currentapiVersion = R.mipmap.image_main_logo;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder notificationChannelBuilder = new NotificationCompat.Builder(OrderMsgService.this, NOTIFICATION_CHANNEL_ID) // For Newer API
.setSmallIcon(currentapiVersion)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setWhen(System.currentTimeMillis())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(orderIntent);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.GREEN);
channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
channel.enableVibration(true);
channel.setDescription("Plan Notifications");
notificationManager.createNotificationChannel(channel);
notificationManager.notify(1, notificationChannelBuilder.build());
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)//for older API
.setSmallIcon(currentapiVersion)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setWhen(System.currentTimeMillis())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(orderIntent);
notificationManager.notify(1, notificationBuilder.build());
}
}
А вот метод onCreate в MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String orderFragment = getIntent().getStringExtra("openOrder");
Log.d(TAG, "order string in main activity:"+orderFragment);
// Launch order if triggered from notification; otherwise open mealplan
if (orderFragment != null) {
if (orderFragment.equals("openOrder")) {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new OrderFragment(), null).commit();
}
} else {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MealplanFragment(), null).commit();
}
}
Мне все равно, если я отправлю логическое, int или строку. Я просто хочу отправить какой-нибудь флаг, чтобы я мог открыть нужный фрагмент.
ОБНОВЛЕНИЕ Вот рабочий код. Я до сих пор не знаю, что я сделал или почему это работает. Но я больше не буду касаться этого и теперь буду просто копировать + вставлять это.
private void showNotification(String title, String body) {
long notificationId = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent openMain = new Intent(this, MainActivity.class);
openMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openMain.putExtra("checkNotification", "openOrder");
PendingIntent orderIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), openMain, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "order intent:"+ orderIntent);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
currentapiVersion = R.drawable.icon_notification;
} else{
currentapiVersion = R.mipmap.image_main_logo;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder notificationChannelBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) // For Newer API
.setSmallIcon(currentapiVersion)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(orderIntent);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
channel.enableVibration(true);
channel.setDescription("Plan Notification");
notificationManager.createNotificationChannel(channel);
notificationManager.notify((int) notificationId, notificationChannelBuilder.build());
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)//for older API
.setSmallIcon(currentapiVersion)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setWhen(System.currentTimeMillis())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(orderIntent);
notificationManager.notify((int) notificationId, notificationBuilder.build());
}
}