Реагировать родные push-уведомления отображаются на консоли на Android, но не показывает уведомление на устройстве или эмуляторе
Я использую FCM для push-уведомлений на реагирующем родном Android, но проблема в том, что я могу получать уведомления, и они появляются на консоли, но не отображаются на устройстве или эмуляторе.
Мой код, как показано ниже
componentDidMount(){
PushNotification.configure({
onRegister: function (token) {
console.log('TOKEN:', token);
},
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
//I AM ABLE TO SEE THE CONSOLE LOG BUT NOTIFICATION DOESN'T APPEAR ON THE DEVICE
},
senderID: "1062347691026",
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true,
});
}
2 ответа
Открытый класс CustomFirebaseMessagingService extends FirebaseMessagingService { String arraysavelist[]; private static final String TAG = CustomFirebaseMessagingService.class.getSimpleName(); Контекстный контекст = это; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Тело сообщения уведомления: " + remoteMessage.getNotification(). GetBody ());
String message = remoteMessage.getNotification().getBody();
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_icon_32)
.setContentTitle("Firebase Push Notification")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Need to create Local Notification then display notification in emulator or android device please use below code inside onNotification method.
var PushNotification = require('react-native-push-notification');
onNotification: function (notification: any) {
const {
foreground,
userInteraction,
message,
id,
data,
...rest
} = notification;
//code for android
if (foreground && !userInteraction) {
PushNotification.localNotification({
...rest,
data,
message,
autoCancel: true,
});
PushNotification.android.setAutoCancel(true);
}
}