【问题标题】:How to add 'big text' or 'inbox style' notification for this Firebase android project?如何为此 Firebase android 项目添加“大文本”或“收件箱样式”通知?
【发布时间】:2016-12-08 02:27:55
【问题描述】:

我正在尝试从 Firebase 控制台发送推送通知,目前我可以从 Firebase 控制台向我的虚拟设备发送一条消息,但如果消息很长,它将不会完全显示在通知栏中。

这是 Firebase 消息传递服务的代码:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.RemoteMessage;

/**
 * Created by filipp on 5/23/2016.
 */
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        showNotification(remoteMessage.getData().get("message"));
    }

    private void showNotification(String message) {

        Intent i = new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("Elit")
                .setContentText(message)
                //.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0,builder.build());
    }


}

感谢 Adib 的详细回答,我已经有一个 PHP 后端服务器,但是每当我尝试从服务器发送长通知时,它不会完全显示,我的问题是我可以只编辑代码的最后一部分,所以我可以使用 inboxStyle 或 bigtextstyle 从我的服务器发送长通知。

private void showNotification(String message) {

    Intent i = new Intent(this,MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Elit")
            .setContentText(message)
            //.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());
}

【问题讨论】:

  • 你的问题到底是什么?
  • 如何编辑我的代码,以便在向虚拟设备发送通知时展开通知。

标签: android firebase push-notification android-notifications firebase-notifications


【解决方案1】:

参考这个link,您仍然无法通过 Firebase 以大通知样式发送推送通知。它将始终采用简单的单一通知样式,除非Firebase 库添加了此功能并开始支持它。

但是,您可以对其进行调整以按照自己的方式工作。但是需要注意,如果您通过发送数据,则此过程是不可能的Firebase 项目控制台的通知部分只有当您的应用有后端并且通过网络发送推送通知时才能这样做。

  1. 使用“data”键(而不是“notification”键)在您的负载中发送通知数据。这样可以确保在收到推送时始终触发 FirebaseMessagingService 类中的 onMessageReceived(RemoteMessage remoteMessage) 方法。如果您在“通知”键中发送数据并且如果您的应用未打开,则会由您的应用以简单的通知样式自动处理(这不是我们想要的)。
  2. 现在我们已经确保所有推送数据都直接发送到 FirebaseMessagingService 类中的 onMessageReceived(RemoteMessage remoteMessage) 方法,而无需自己创建通知,您需要创建一个通知收到的数据。以下是用于实现该目的的示例代码:

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, whatToOpen, PendingIntent.FLAG_ONE_SHOT);
    
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.push_icon_small)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.push_icon_large))
            .setSound(defaultSoundUri)
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setContentIntent(pendingIntent);
    
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    
    }
    

这部分是使通知变大的原因。

.setStyle(new NotificationCompat.BigTextStyle().bigText(message))

现在您甚至可以在推送通知中发送文章! 希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2018-10-15
    相关资源
    最近更新 更多