Я использую Уведомление FCM, и он работает отлично.проблема в том, что я не могу показать всплывающее уведомление, если приложение закрыто.уведомление отображается только в истории уведомлений.Я хочу показать всплывающее окно, как WhatsApp, если приложение не открыто.
Если приложение открыто.тогда это работает отлично.Метод onMessageReceived вызывается только при открытом приложении.
Вот мой код.
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
NotificationCompat.Builder mBuilder;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
if (remoteMessage.getNotification() != null) {
String noti_body = remoteMessage.getNotification().getBody();
String noti_title = remoteMessage.getNotification().getTitle();
mBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
.setSmallIcon(R.mipmap.logo)
.setContentTitle(noti_title)
.setContentText(noti_body)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
.setColor(getResources().getColor(R.color.colorAccent))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(true);
Intent intent = new Intent(this, MainActivity.class);
@SuppressLint("WrongConstant")
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
php код:
$data = array
(
"activity" => $activity_to_open
);
$notification = array('title' =>$title , 'body' => $message,'click_action' => '.MainActivity' ,'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'data' => $data, 'priority'=>'high');