Ad

Could Not Get Notifications If I Run My App On Huawei Device

- 1 answer

My problem is if I run my application on Huawei device I couldn't get any notification, but if I run my app on another device it works fine, and you find below the notification code in my FirebaseMessagingService:

    private void displayNotification(RemoteMessage.Notification notification, Map<String, String> data) {
        Random generator = new Random();
        String id = data.get("element");
        String type = data.get("type");
        Intent intent;
        intent = new Intent(this, Home.class);
        intent.putExtra("retour", type + "test");
        intent.putExtra("element_id", id);
        intent.setAction(Long.toString(System.currentTimeMillis()));
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//RingtoneManager.TYPE_NOTIFICATION
        long[] pattern = {500, 500, 500, 500, 500, 500, 500, 500, 500};
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this);
        notificationbuilder.setContentTitle(notification.getTitle());
        notificationbuilder.setContentText(notification.getBody());
        notificationbuilder.setAutoCancel(true);
        notificationbuilder.setVibrate(pattern);
        notificationbuilder.setSound(defaultSoundUri);
        notificationbuilder.setSmallIcon(R.drawable.icoapp_and);
  notificationbuilder.setColor(getResources().getColor(R.color.colorAccent));
        notificationbuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(generator.nextInt(), notificationbuilder.build());   }
Ad

Answer

Try the following code to add the channel to NotificationManager:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     mChannel = new NotificationChannel(idChannel, 
     context.getString(R.string.app_name), importance);
     // Configure the notification channel.
     mChannel.setDescription(context.getString(R.string.alarm_notification));
     mChannel.enableLights(true);
     mChannel.setLightColor(Color.RED);
     mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 
     300, 200, 400});
     mNotificationManager.createNotificationChannel(mChannel);
} else {
     builder.setContentTitle(context.getString(R.string.app_name))
          .setPriority(NotificationCompat.PRIORITY_HIGH)
          .setColor(ContextCompat.getColor(context, R.color.transparent))
          .setVibrate(new long[]{100, 250})
          .setLights(Color.YELLOW, 500, 5000)
          .setAutoCancel(true);
}
Ad
source: stackoverflow.com
Ad