android - 如何使用自定义布局显示 Firebase 通知?

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

火力基地 有一个默认的简单通知布局作为 Android 的默认布局。如何将其更改为自定义布局并在生成时显示通知。

最佳答案

Firebase 消息 服务 写下:

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

关于android - 如何使用自定义布局显示 Firebase 通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799026/

相关文章:

iphone - 将 Urbanairship 集成到我的 iOS 应用程序中所需的最少文件集是什么?

ios - Localytics 推送通知文本限制

iOS:Objective -C 如何在应用程序处于后台模式时更改推送通知声音有效负载?

java - 如何与 friend 分享推荐代码和 Play 商店应用程序

android - 如何在运行时向 ListView 添加项目?

objective-c - 如何在 OS X Cocoa 应用程序中使用 Google 授权用户

java - 如何在 Android 中提供 Firestore 身份验证?

c# - 无法通过 Unity 将数据写入 Firebase 数据库

android - 如何在 Android 上修复混淆器 NoSuchMethodException

android - Truecaller API 中的 APPKEY 是什么?