1. Я удалил ниже зависимости
реализация 'com.google.firebase: firebase-analytics: 17.0.1'
реализация 'com.google.firebase: firebase-messaging: 20.1. 0 '
реализация' com.google.firebase: firebase-core: 17.2.1 '
2. Добавлена
реализация 'com.google. android .gms: play-services-gcm: 17.0.0'
3. В Androidmanifest.xml
добавлено
<permission
android:name="packagename.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="packagename.permission.C2D_MESSAGE" />
В application
тег
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<!-- for Gingerbread GSF backward compat -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="packagename" />
</intent-filter>
</receiver>
<service
android:name=".utils.PushNotificationReceiver" android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
4. Создан новый класс PushNotificationReceiver
extends GcmListenerService
переопределить onMessageReceived
@Override
public void onMessageReceived(String from, Bundle data) {
try {
if (data.containsKey("data")) {
JSONObject jsonObject = new JSONObject(data.getString("data"));
sendNotification(jsonObject.getString("title"), jsonObject.getString("message"),
jsonObject.getString("channel"));
}
} catch (Exception ex) {
}
}
private void sendNotification(String title, String message, String channel) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channel,
channel, NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
Intent intent = new Intent(this, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channel);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.trot_icon_with_space)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setContentInfo("Info");
if (notificationManager != null) {
int pushNotificatonId = new Random().nextInt(10000);
notificationManager.notify(/*notification id*/pushNotificatonId, notificationBuilder.build());
}
}