Android推送通知,当应用程序关闭时,我会得到不同的风格

时间:2017-06-27 10:36:40

标签: android firebase push-notification firebase-cloud-messaging

我正在使用FCM进行通知,其中一切正常,但是直到应用程序打开,一旦我杀死(关闭)应用程序或在后台,我得到默认样式的通知,任何人都可以帮我设置此通知应用程序关闭时的样式(或任何其他建议)。 请帮助我,提前谢谢

这是我的代码

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    String title = "";
    if (remoteMessage.getNotification().getTitle() != null){
        title = remoteMessage.getNotification().getTitle();
    }

    String message = "";
    if (remoteMessage.getNotification().getBody() != null){
        message = remoteMessage.getNotification().getBody();
    }

    Log.e("notification","recieved");


    sendNotification(title, message);

}



private void sendNotification(String title, String message) {

    Intent intent = new Intent(this, MainActivity2.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    int color=getResources().getColor(R.color.dot_dark_screen2);

    NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.account_outline)
            .setColor(color)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setAutoCancel(true)
            .setSound(notificationSound)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());

enter image description here

1 个答案:

答案 0 :(得分:1)

我已经在这里发布了一个很长的解释: Android notification icon issue

TL; DR:

您的问题很可能是通知消息数据消息之间存在差异。

请阅读:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

  

当您希望FCM处理显示时,请使用通知消息   代表您的客户端应用程序发送通知。在您使用时使用数据信息   想要处理客户端应用上的消息。