Probléme avec notification push androidStudio

CR16_Padawan Messages postés 61 Date d'inscription mercredi 7 juin 2023 Statut Membre Dernière intervention 22 septembre 2024 - 22 nov. 2023 à 17:40
BunoCS Messages postés 15952 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 5 mai 2025 - 27 nov. 2023 à 08:43

Bonjour ,

une fois de plus je souhaiterai obtenir un peu de vos lumières.

je dois envoyer des notifications push avec mon appli mais le receveur n'a pas la notification sur son appareil.

voici quelques éléments

MessagingService.java

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        //Log.d("FCM","Message: " + remotemessage.getNotification().getBody());
        User user = new User();
        user.id = remoteMessage.getData().get(Constants.KEY_USER_ID);
        user.name = remoteMessage.getData().get(Constants.KEY_NAME);
        user.token = remoteMessage.getData().get(Constants.KEY_FCM_TOKEN);

        int notificationId = new Random().nextInt();
        String channelId = "chat_message";

        Intent intent = new Intent(this, ChatActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(Constants.KEY_USER, user);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
        builder.setSmallIcon(R.drawable.ic_notification);
        builder.setContentTitle(user.name);
        builder.setContentText(remoteMessage.getData().get(Constants.KEY_MESSAGE));
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(
                remoteMessage.getData().get(Constants.KEY_MESSAGE)
        ));
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence channelName = "Chat Message";
            String channelDescription = "Ce canal de notification est utilisé pour les notifications message chat";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            channel.setDescription(channelDescription);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        notificationManagerCompat.notify(notificationId, builder.build());
    }

AndroidManifest

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

...

        <service
            android:name=".firebase.MessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Est-ce un problème d'autorisation car dans la dernière partie du code java, si je ne mets pas la condition "if", la dernière ligne se souligne en rouge et je peux lire ce conseille en survolant la ligne

"Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException"

par avance merci à vous.


A voir également:

1 réponse

BunoCS Messages postés 15952 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 5 mai 2025 3 914
27 nov. 2023 à 08:43

Hello,

Est-ce un problème d'autorisation

Pour le savoir, tu peux activer ou désactiver cette autorisation directement dans les infos de l'app.

Quel service de push utilises-tu ? Généralement, il y a toujours un guide développeur qui est fourni.


0