I am unable to run firebase messaging on device
this is MyFirebaseMessagingService.java
package com.xyz;
import static androidx.core.content.ContextCompat.getSystemService;
import android.app.AlertDialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Debug;
import android.util.Log;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.io.Console;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingSvc";
private static final String CHANNEL_ID = "com.xyz.notification";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Firebase Messaging service created");
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Check if the message contains data
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
//show error
Toast.makeText(this, "Error processing data payload", Toast.LENGTH_SHORT).show();
// Handle the data payload here
// You can access the data payload using remoteMessage.getData()
}
// Check if the message contains a notification payload
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message notification body: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
// Handle the notification payload here
// You can access the notification payload using remoteMessage.getNotification()
}
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.d(TAG, "Refreshed token: " + token);
}
private void showNotification(String title, String message) {
Log.d(TAG, "Showing notification with title: " + title + " and message: " + message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Aayu Authenticator Notifications";
String description = "Notifications for Aayu Authenticator";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system
notificationManager.createNotificationChannel(channel);
}
// Show the notification
notificationManager.notify(12344, builder.build());
}
}
below is androidmanifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.xyz"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="false" />
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
this is the files associated and i added the log code to log but not working please check and help out in this
logging.,
setting export to true and false
i want to get token to receive notification and after running the code fcm console says no fcm token
if the code seems fine then why it is not generating the token or seems like the file is not called or running