作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建一个服务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/
我创建一个服务extend FirebaseMessiginService,receive预计会收到数据通知。 onMessageReceived 很好地接收数据负载并正确提取数据。然后调用 send
我试图重建一个包含 tess-two 作为库的 android 应用程序。按原样使用时,出现此错误 java.lang.NoSuchFieldError: no field with name='mN
我知道有很多关于这个问题的帖子,但其中任何一个都可以帮助我。 嗯,我有 Mac OS X Lion 和 Xcode 4,当我仅创建一个文本字段并尝试在模拟器中写入内容时,我收到错误“线程 1 程序收到
我有如下所示的生产者消费者示例程序 如何在我的 Consumer Thread 类中放置一个 Condition,以便如果我在 1 分钟内没有收到来自生产者的数据,我需要记录该数据?? 这是我的生产者
我是一名优秀的程序员,十分优秀!