Where the mind is without fear..



buildscript {
repositories {
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.8'
}
}
allprojects {
repositories {
google()
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:26.6.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
}
package your.package.name;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessageReceiver extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
if(remoteMessage.getNotification() != null){
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
addNotification(body);
}
}
@Override
public void onNewToken(String token){
}
private void addNotification(String msg){
//String channel_i = "Your Notification";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("Test Notification")
.setContentText(msg)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setChannelId(AdmActivity.CHANNEL_ID);
Intent notificationIntent = new Intent(this, MyActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("message",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
….. ……. ….. ….. …….
<service android:name=".FirebaseMessageReceiver">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
