Reputation: 71
I have added Firebase Cloud Messaging to my app and I setuped Firebase messaging services. And it was working fine and app was receiving notification even when the app was not running on background. But from last 2 days App is not receiving any notification. I don't know what happened with my code.
FcmMessagingService.java
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size()>0){
String title,message,img_url;
title = remoteMessage.getData().get("title");
message = remoteMessage.getData().get("message");
img_url = remoteMessage.getData().get("img_url");
Intent intent = new Intent(this, Notification.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.app_icon_round);
Uri sounduri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "MY_CH_ID";
CharSequence name = "Product";
String description = "Notifications regarding our products";
int importance = NotificationManager.IMPORTANCE_MAX;
@SuppressLint("WrongConstant")
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(sounduri,attributes);
notificationManager.createNotificationChannel(mChannel);
}
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MY_CH_ID");
builder.setContentTitle(title);
builder.setSubText(message);
builder.setContentIntent(pendingIntent);
builder.setSound(sounduri);
builder.setSmallIcon(R.drawable.fcmicon);
builder.setLargeIcon(rawBitmap);
builder.setChannelId("MY_CH_ID");
ImageRequest imageRequest = new ImageRequest(img_url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(response));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}, 0, 0, null, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getmInstance(this).addToRequestQue(imageRequest);
}
}
}
In my main activity I have subscribed to a topic.
FirebaseMessaging.getInstance().subscribeToTopic("PARETHUMUKAL_CHURCH");
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parethumukal">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/app_icon_round"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Archived"
android:configChanges="orientation"
android:screenOrientation="portrait" ></activity>
<activity android:name=".Ebook"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Prayer"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Churchtiming"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Forgotpassword"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Privacypolicy"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Directoryquick"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Churchinside"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Extras"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Shrine"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Youtubeplayer" />
<activity
android:name=".Video"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Driveactivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Audios"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Images"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Profileregistration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Registration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Milandetails"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Ambulance"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Milan"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Feast"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Priest"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Churchadmin"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Youthassociation"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Vanithasamajam"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Familyunit"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Sundayschool"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Leaders"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".History"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Livestream" />
<activity
android:name=".Notification"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Myprofile"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Quicklinks"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Services"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Directory"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Gallery"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Groups"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Administration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Church"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Aboutus"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".WelcomeActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Homewindow"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Login"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Splashscreen"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".FcmMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
build.gradle implementation
implementation 'com.google.firebase:firebase-messaging:19.0.1'
I dont know what is the problem.
Upvotes: 0
Views: 1255
Reputation: 8844
Everything is fine on your end. You just need to send Firebase Notification Messages see here.
As you can see there is Notification
JSON object and it includes title
and body
keys which will be displayed in the notification title.
Sample:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
Don't forget to Replace token
with your FCM device token.
Upvotes: 1