I'm just using firebase, now I've managed to create user authentication and have a few push notification questions in the firebase.
Before I followed the tutorial here, and it worked I was very happy. But apparently after I logout and try push notification, I still still receive the notification.
So my question is also pretty much about push notifications, namely:
- How to make the notification only enter when I'm logged in?
- How do I target the notification with the registered UID only?
- Is it necessary to make notifications from firebase, so it can't be as straightforward as an automatic chat application?
Here's the code I've made:
MyFirebaseInstanceldService.java
public class MyFirebaseInstanceldService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String refreshedToken) {
}
}
MyFirebaseMessaginService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, HotelManagementActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_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);
notificationBuilder.setSmallIcon(R.drawable.rotarylogo);
notificationBuilder.setContentTitle("Rotary Barcode");
notificationBuilder.setContentText(messageBody);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.notify(0, notificationBuilder.build());
}
}
If there is anything I still need to add, just say for the other code. I thank you very much for wanting to help me who is still a beginner.