如何使用自定义布局显示firebase通知?

时间:2017-05-05 07:29:47

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

Firebase 的默认简单通知布局为Android默认布局。如何将其更改为自定义布局并在生成时显示通知。

2 个答案:

答案 0 :(得分:0)

在服务器端,删除notification属性 当您发送没有notification属性的通知时,firebase将无法处理通知。 然后,您可以扩展FirebaseMessagingService来处理通知。

不要忘记在清单中注册服务。

答案 1 :(得分:0)

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());
}