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/ –