Настройка уведомлений в своем мессенджере
Столкнулся с проблемой, никак не запускаются уведомления в чате. Написал программу с помощью Firebase, которая выглядит как глобальный чат, хотел добавить push-уведомления. Код обработчика уведомлений:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData() != null)
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
Map<String,String> data = remoteMessage.getData();
String title = data.get(R.id.tvUser);
String content = data.get(R.id.tvMessage);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "Riazon Chat";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
@SuppressLint("WrongConstant")
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
"Riazon Chat",
NotificationManager.IMPORTANCE_MAX);
notificationChannel.setDescription("Riazon Chat");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.WHITE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
androidx.core.app.NotificationCompat.Builder notificationBuilder = new androidx.core.app.NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(android.R.drawable.ic_notification_overlay)
.setTicker("Hearty365")
.setContentTitle(title)
.setContentText(content)
.setContentInfo("info");
notificationManager.notify(101, notificationBuilder.build());
}
}
Манифест выглядит таким образом:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.riazon.chat">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Service.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
Как быть? Чат основан на realtime database в Firebase'е. Перерыл множество сайтов, но ответ на свой вопрос так и не нашел. Минимальная версия Андроида - 5.1, тестирую на 9 и 10