如果应用关闭,则带有POP-UP的Fcm通知

时间:2018-12-04 11:19:24

标签: php android push-notification

我正在使用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');

0 个答案:

没有答案