Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何添加';大文本';或';收件箱样式';Firebase android项目的通知?_Android_Firebase_Push Notification_Android Notifications_Firebase Notifications - Fatal编程技术网

如何添加';大文本';或';收件箱样式';Firebase android项目的通知?

如何添加';大文本';或';收件箱样式';Firebase android项目的通知?,android,firebase,push-notification,android-notifications,firebase-notifications,Android,Firebase,Push Notification,Android Notifications,Firebase Notifications,我正在尝试从Firebase控制台发送推送通知,目前我可以从Firebase控制台向虚拟设备发送消息,但如果消息很长,它将不会完全显示在通知栏中 这是Firebasemessagingservice的代码: import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v4.app.Notificatio

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

这是Firebasemessagingservice的代码:

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());
}
鉴于此,您仍然无法以大通知样式通过Firebase发送推送通知。除非Firebase库添加此功能并开始支持它,否则推送通知将始终采用简单的单一通知样式

但是,您可以调整它以按自己的方式工作。但是要注意的是,如果您通过Firebase项目控制台的通知部分发送数据,则此过程不可能进行只有当您的应用程序具有后端并且推送通知通过web发送时,才能执行此操作。

  • 使用“数据”键而不是“通知”键在有效负载中发送通知数据。这可确保在收到推送时始终触发FirebaseMessagingService类中的onMessageReceived(RemoteMessage RemoteMessage)方法。如果您在“通知”键中发送数据,并且您的应用程序未打开,它将由您的应用程序以简单的通知样式自动处理(这不是我们想要的)
  • 现在,我们已经确保所有推送数据都直接到达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))
    

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

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