Created
December 21, 2017 07:57
-
-
Save yusufcakal/2c8fbc5fbdba0cc95380bae14d462fca to your computer and use it in GitHub Desktop.
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
package yusufcakal.com.cloudmessagging; | |
/** | |
* Created by Yusuf on 19.12.2017. | |
*/ | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
import com.google.firebase.messaging.FirebaseMessagingService; | |
import com.google.firebase.messaging.RemoteMessage; | |
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
private static final String TAG = "MyFirebaseMsgService"; | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
super.onMessageReceived(remoteMessage); | |
Log.d(TAG, "From: " + remoteMessage.getFrom()); | |
if (remoteMessage.getData().size() > 0) { | |
Log.d(TAG, "Message data payload: " + remoteMessage.getData()); | |
} | |
if (remoteMessage.getNotification() != null) { | |
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); | |
} | |
sendNotification(remoteMessage.getNotification()); | |
} | |
private void sendNotification(RemoteMessage.Notification message) { | |
Intent intent = new Intent(this, MainActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT); | |
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) | |
.setSmallIcon(R.drawable.ic_notifications_black_24dp) | |
.setContentText(message.getBody()) | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setVibrate(new long[]{1000,1000}) | |
.setContentIntent(pendingIntent); | |
if(message.getTitle()!=null){ | |
notificationBuilder.setContentTitle(message.getTitle()); | |
}else{ | |
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name)); | |
} | |
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment