package com.ramzey.fcmpush2023.FCM;
import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.util.Log;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.ramzey.fcmpush2023.MainActivity; import com.ramzey.fcmpush2023.R;
public class FirebaseMsgService extends FirebaseMessagingService { @Override public void onNewToken(@NonNull String token) { super.onNewToken(token); Log.d("RefreshToken", token); }
@Override public void onMessageReceived(@NonNull RemoteMessage message) { super.onMessageReceived(message); RemoteMessage.Notification noti = message.getNotification(); if (noti != null) { pushNotification(noti.getTitle(), noti.getBody()); } }
public void pushNotification(String title, String body) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification;
String channelId = "default"; Intent intent = new Intent(this, MainActivity.class);
Intent iNotify = new Intent(this, MainActivity.class); iNotify.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // PendingIntent pi = PendingIntent.getActivity(this, 100, iNotify, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); } else { pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT); }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "default"; String description = "Channel for Push Notifications"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, name, importance); channel.setDescription(description); if (notificationManager != null) { notificationManager.createNotificationChannel(channel); }
notification = new Notification.Builder(this) .setSmallIcon(R.drawable.a) .setContentIntent(pendingIntent) .setContentTitle(title) .setSubText(body) .setAutoCancel(true) .setChannelId(channelId) .build(); } else { notification = new Notification.Builder(this) .setSmallIcon(R.drawable.a) .setContentIntent(pendingIntent) .setAutoCancel(true) .setContentTitle(title) .setSubText(body) .build(); } if (notificationManager != null) { notificationManager.notify(1, notification); }
} } |