Reputation: 178
I have implemented push notifications through Firebase in my app. The notification is coming even when the notification is disabled from settings. The classes I have implemented for Firebase are:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
String to="";
to = remoteMessage.getData().get("key1");
//when the notification is disabled then also the notification is coming
if(notification_enable) {
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),to);
}
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String title,String messageBody,String to) {
Intent intent = new Intent(this, Splash_Activity.class);
intent.putExtra("key1",to);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentText(messageBody)
.setAutoCancel(true)
.setColor(this.getResources().getColor(R.color.colorAccent))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
And included the classes in manifest as:
<permission
android:name="com.pixelpoint.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.pixelpoint.permission.C2D_MESSAGE" />
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Upvotes: 8
Views: 20394
Reputation: 2982
Are you sending the notification using the Firebase Console?
These notifications will be handled by the system if the app is in the background and your MyFirebaseMessagingService
wont receive a callback. Your code that does a client side check for if a user is registered in local settings to receive a notification won't work for all cases. (More about background processing here https://firebase.google.com/docs/cloud-messaging/android/receive)
My suggestion for this is to create a topic and automatically subscribe the user to that topic straight after registering the user:
FirebaseMessaging.getInstance().subscribeToTopic("news");
Then when the user toggles the notification off, unsubscribe them from the topic.
FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
This will remove them from the list on the server, and you wont be relying on client side logic to filter out unwanted notifications.
Then when sending notification to the client from Firebase Console, you should then only target those users that are registered for that topic.
More about Topic Messaging here - https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Upvotes: 15