0

Firebase具有默认的简单通知布局,android默认为一次。如何更改为自定义布局,并在生成时显示通知。如何使用自定义布局显示Firebase通知?

+0

U表示添加图片或图标,您的通知 –

+0

NOP兄弟创建定制商品通知或者可以说,倒塌的通知。 –

回答

1

在FirebaseMessaging服务编写以下

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    if (remoteMessage.getData().size() > 0) { 


     try { 

     JSONObject jsonObject = new JSONObject(remoteMessage.getData()); 
      Log.e("Tag",remoteMessage.getData().toString()); 


      sendNotification(remoteMessage.getData().toString()); 


     } catch (Exception e) { 


     } 


    } 
private void sendNotification(String msg) { 
    Intent intent = new Intent(this, NewTransactionsHistActivity.class);  
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(100) , intent, 
      PendingIntent.FLAG_ONE_SHOT); 
    long when = System.currentTimeMillis(); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this); 
    mNotifyBuilder.setVibrate(new long[] { 1000, 1000,1000,1000,1000,1000}); 
    boolean lollipop = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); 
    if (lollipop) { 

     mNotifyBuilder = new NotificationCompat.Builder(this) 
       .setContentTitle(getString(R.string.app_name)) 
       .setStyle(
         new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
       .setContentText(msg) 
       .setColor(Color.TRANSPARENT) 
       .setLargeIcon(
         BitmapFactory.decodeResource(
           getResources(), 
           R.drawable.rlogo)) 
       .setSmallIcon(R.drawable.ic_icon_lollipop) 

       .setWhen(when).setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

    } else { 

     mNotifyBuilder = new NotificationCompat.Builder(this) 
       .setStyle(
         new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
       .setContentTitle(getString(R.string.app_name)).setContentText(msg) 
       .setSmallIcon(R.drawable.rlogo) 
       .setWhen(when).setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

    } 


    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(new Random().nextInt(100) /* ID of notification */, mNotifyBuilder.build()); 
} 
+0

** onMessageReceived **方法触发器当应用程序在前台。 如果应用程序在后台运行,它将不会触发。 检查[本答](https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background)。 –

0

在你的服务器端,删除notification attirbute。
当您发送没有notification属性的通知时,Firebase将不处理通知。 然后,您可以扩展FirebaseMessagingService来处理通知。 不要忘记在清单中注册服务。

+0

我收到通知但我想更改它与自定义布局如何处理它 –

+0

检查此https://www.laurivan.com/android-notifications-with-custom-layout/ –