Created
March 23, 2018 15:00
-
-
Save fevziomurtekin/fcbd3ba2e83925c459b22d5614c3ea00 to your computer and use it in GitHub Desktop.
MyFirebaseService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
private static final String TAG = "MyFirebaseMsgService"; | |
private int id=32489; | |
/** | |
* Called when message is received. | |
* | |
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging. | |
*/ | |
// [START receive_message] | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
// [START_EXCLUDE] | |
// There are two types of messages data messages and notification messages. Data messages are handled | |
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type | |
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app | |
// is in the foreground. When the app is in the background an automatically generated notification is displayed. | |
// When the user taps on the notification they are returned to the app. Messages containing both notification | |
// and data payloads are treated as notification messages. The Firebase console always sends notification | |
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options | |
// [END_EXCLUDE] | |
String link="",message=""; | |
// TODO(developer): Handle FCM messages here. | |
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ | |
Log.d(TAG, "From: " + remoteMessage.getFrom()); | |
// Check if message contains a data payload. | |
if (remoteMessage.getData().size() > 0) { | |
message = remoteMessage.getData().get("devices"); | |
if(message.contains("global")) | |
link="main"; | |
else if(message.contains("sensor")) | |
link="sensor"; | |
else if(message.contains("actuator")) | |
link="actuator"; | |
} | |
// Check if message contains a notification payload. | |
if (remoteMessage.getNotification() != null) { | |
message= remoteMessage.getNotification().getBody(); | |
} | |
if(!message.isEmpty()) | |
showNotification(this,message,link); | |
// Also if you intend on generating your own notifications as a result of a received FCM | |
// message, here is where that should be initiated. See sendNotification method below. | |
} | |
// [END receive_message] | |
private void showNotification(Context context, String message, String link) { | |
String title = context.getString(R.string.app_name); | |
if(message==null)return; | |
String tick; | |
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
tick=message.length()<50?message:message.substring(0,45)+"..."; | |
NotificationCompat.Builder builder = | |
new NotificationCompat.Builder(context) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setColor(context.getResources().getColor(R.color.white)) | |
// .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) | |
.setVibrate(new long[]{0,150,100,150,100,150}) | |
.setAutoCancel(true) | |
.setWhen(System.currentTimeMillis()) | |
.setTicker(tick) | |
.setLights(Color.GREEN, 500, 1500) | |
.setContentTitle(title) | |
.setSound(defaultSoundUri); | |
if(message.length()>30)builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); | |
builder.setContentText(message); | |
Intent notificationIntent = new Intent(this, MainActivity.class); | |
if(link!=null) | |
notificationIntent.putExtra(C.KEY_LINK,link); | |
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | |
| Intent.FLAG_ACTIVITY_NEW_TASK); | |
PendingIntent contentIntent = PendingIntent.getActivity(this, id, notificationIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
builder.setContentIntent(contentIntent); | |
// Add as notification | |
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
manager.notify(id, builder.build()); | |
} | |
} | |
/* | |
* String link="",message="",topic=""; | |
// TODO(developer): Handle FCM messages here. | |
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ | |
Log.d(TAG, "From: " + remoteMessage.getFrom()); | |
topic=remoteMessage.getFrom(); | |
// Check if message contains a data payload. | |
if (remoteMessage.getData().size() > 0) { | |
message = remoteMessage.getData().get("devices"); | |
if(message.contains("global")) | |
link="main"; | |
else if(message.contains("sensor")) | |
link="sensor"; | |
else if(message.contains("actuator")) | |
link="actuator"; | |
} | |
// Check if message contains a notification payload. | |
if (remoteMessage.getNotification() != null) { | |
message= remoteMessage.getNotification().getBody(); | |
} | |
if(!message.isEmpty()) { | |
showNotification(this, message, link); | |
} | |
// Also if you intend on generating your own notifications as a result of a received FCM | |
// message, here is where that should be initiated. See sendNotification method below. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment