gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:52:10 25 4
gpt4 key购买 nike

火力基地 有一个默认的简单通知布局作为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com