FirebaseInstantIdService.java -
package com.cryptobites.service;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class FirebaseInstantIdService extends FirebaseInstanceIdService {
private static String refreshedToken;
@Override public void onTokenRefresh() {
super.onTokenRefresh();
refreshedToken = FirebaseInstanceId.getInstance().getToken();
}
public static String getFirebaseId() {
return refreshedToken;
}
}
-------------------------------------------------------------------------------------
FirebaseNotificationService.java:-
package com.cryptobites.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import com.cryptobites.view.activity.HomeActivity;
import com.cryptobites.view.activity.LoginActivity;
import com.cryptobites.R;
import com.cryptobites.view.activity.HomeActivity;
import com.cryptobites.view.activity.LoginActivity;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseNotificationService extends FirebaseMessagingService {
@Override public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getNotification().getBody());
}
public void showNotification(String string)
{
SharedPreferences sharedPreferences= getSharedPreferences("id",MODE_PRIVATE);
String user_id = sharedPreferences.getString("user_id","");
if(user_id.isEmpty()){
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, LoginActivity.class),0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Cryptobites")
.setContentText(string)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notification);
}
else {
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this, HomeActivity.class),0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Cryptobites")
.setContentText(string)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notification);
}
}
}
------------------------------------------------------------------------------------
AndroidManifest.xml:-
<service android:name=".service.FirebaseInstantIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".service.FirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
------------------------------------------------------------------------------------------
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-core:16.0.6'
------------------------------------------------------------------------------
Comments
Post a Comment