gpt4 book ai didi

android - Kotlin : FirebaseMessigingService is recive the message, 但未显示通知

转载 作者:行者123 更新时间:2023-11-30 00:02:36 31 4
gpt4 key购买 nike

我创建一个服务extend FirebaseMessiginService,receive预计会收到数据通知。

onMessageReceived 很好地接收数据负载并正确提取数据。然后调用 sendNotifcation()。但没有显示通知。

这是我的发送通知函数

private fun sendNotification(item: Item) {

val intent = Intent(this, ItemNotificationActivity::class.java)

intent.putExtra(ItemDetailFragment.ARG_ITEM, item)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
intent.setAction( System.currentTimeMillis().toString())

val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)

// String channelId = getString(R.string.default_notification_channel_id);
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, "channelId")
.setSmallIcon(R.drawable.ic_new_releases_black_24dp
)
.setContentTitle(getString(R.string.app_name))
.setContentText(item.title.trim())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("channelId" + item.id,
item.title,
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
val notification = notificationBuilder.build()
notification.flags = notification.flags or Notification.FLAG_AUTO_CANCEL

notificationManager.notify(item.id /* ID of notification */, notification)
}

我的 sendNotification 有什么问题?

最佳答案

 public class MyFireBaseMessagingService extends FirebaseMessagingService {

@Override
public void onCreate() {
super.onCreate();
}

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("Notification", "From : " + remoteMessage.getFrom());
Log.e("Notification", "Data : " + remoteMessage.getData().toString());
String type = remoteMessage.getData().get("notificationType");
try {
sendNotification(remoteMessage);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(RemoteMessage remoteMessage) throws UnsupportedEncodingException {
String title, message;
title = getString(R.string.app_name);
message = "" + remoteMessage.getData().get("text"); //instead of text you can have your key getting received in notification

// Pending intent will perform redirection on clicking Notification

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("fromNotification", true);
PendingIntent contentIntent = PendingIntent.getActivity(this,
(int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_CANCEL_CURRENT);


// Let's create notification to be displayed


NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(message);
Bitmap remotePicture = null; // This will be used only when you will have image url in notification and want to display image as a notification.

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // incase you want to set your custom sound for notification

notificationBuilder
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.app_purple))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(contentIntent);

// If there is an image URL to be displayed, get the bitmap from URL and pass it to notification builder

if (remoteMessage.getData().containsKey("image")) {
try {
remotePicture = BitmapFactory.decodeStream((InputStream) new URL(remoteMessage.getData().get("image")).getContent());
notiStyle.bigPicture(remotePicture);
} catch (IOException e) {
e.printStackTrace();
}
if (remotePicture != null) notificationBuilder.setStyle(notiStyle);
}

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

// If you are using device with OS OREO or ABOVE

if (VERSION.SDK_INT >= VERSION_CODES.O) {
String channelId = "your_app_notificaiton_channel_id";
CharSequence channelName = "Your App Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setShowBadge(true);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();

notificationChannel.setSound(soundUri, audioAttributes);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
notificationManager .createNotificationChannel(notificationChannel);
notificationBuilder.setChannelId(channelId);
}

notificationManager.notify((int) System.currentTimeMillis() /* ID of notification */, notificationBuilder.build());
}
}

关于android - Kotlin : FirebaseMessigingService is recive the message, 但未显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49642592/

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