Gautam Bhateja
Gautam Bhateja

Reputation: 81

My Notification Channel is not being created

Whenever I send a notification through fcm.

It says it will use the default from manifest or the android app, I also have mentioned my channel in manifest as default. It just says that the channel is not created by the app.

Log says

W/FirebaseMessaging: Notification Channel requested (message_notification) has not been created by the app. Manifest configuration, or default, value will be used.

This is my onMessageReceived function.

override fun onMessageReceived(p0: RemoteMessage) {
        super.onMessageReceived(p0)
        sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+":/ /"+applicationContext.packageName+"/"+R.raw.alert)
        Log.d("notofiction Nul", sound.toString())

        if(p0.notification!=null){

            val title = p0.data["title"]
            val message = p0.data["body"]
            Log.d("notottg",title+message)
            buildNotificationChannel()
 if(buildNotificationChannel()){
                generateNotification(title!!,message!!)
            }

        }
        else{
            val title = p0.data["title"]
            val message = p0.data["body"]
            Log.d("notottg",title+message)
            buildNotificationChannel()

            if(buildNotificationChannel()){
                generateNotification(title!!,message!!)
            }

        }


    }

This is my createNotificationChannel function

 private fun buildNotificationChannel():Boolean {


        /*var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build()
        sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
        Log.d("notisound",sound.toString())*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val CHANNEL_ID =  "merchant_notification"
            val CHANNEL_NAME = "Merchant Notification"
            var notificationChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
                vibrationPattern=longArrayOf(400, 400, 400, 400, 500, 400, 400, 400, 400)
                enableVibration(true)
            }
            notificationManager= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)





            //notificationChannel.setSound(sound,audioAttributes)

        }
        return true

    }

And this is my generateNotification function

fun generateNotification(title: String, message: String) {

        val intent = Intent(this@CustomMessagingService, MainActActivity::class.java)
        var pendingIntent = PendingIntent.getActivity(this@CustomMessagingService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        var builder: NotificationCompat.Builder =
            NotificationCompat.Builder(this, "merchant_notification")
        var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build()
        sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
        Log.d("notisound",sound.toString())

        builder.setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(pendingIntent)
            .setSound(sound)

        notification = builder.build()
        Log.d("notot", notification.toString() + "   " + sound.toString())
        notificationManager.notify(1, notification)



        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            var nb: Notification.Builder = Notification.Builder(this)
            nb.setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .setSound(sound, audioAttributes)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent)
                .build()
            notification = nb.notification
            notification.flags = Notification.FLAG_AUTO_CANCEL
            notificationManager.notify(0, notification)
        }


    }

My Manifest Code

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

            </intent-filter>
        </service>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="merchant_notification" />

Upvotes: 1

Views: 1740

Answers (1)

Avital
Avital

Reputation: 567

It works for me:

public class MyFirebaseMessagingService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // channel for notifications

            NotificationManager notificationManager = getSystemService(NotificationManager.class);

            if (notificationManager != null) {
                List<NotificationChannel> channelList = notificationManager.getNotificationChannels();

                for (int i = 0; channelList != null && i < channelList.size(); i++)
                    notificationManager.deleteNotificationChannel(channelList.get(i).getId());
            }

            String name = getString(R.string.notification_channel_name);
            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel channel = new NotificationChannel(getString(R.string.notification_channel_id), name, importance);
            
            assert notificationManager != null;
            notificationManager.createNotificationChannel(channel);
        }

        Intent intent;
        intent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntentWithParentStack(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
                .setSmallIcon(R.drawable.logo)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentTitle(message.getData ().get ("myBody"))
                .setContentIntent(pendingIntent);


        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());
    }

    @Override
    public void onNewToken(@NonNull String token) {
    }
}

Manifest:

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

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/notification_channel_id" />

Upvotes: 1

Related Questions