Skip to content

Instantly share code, notes, and snippets.

@vitqst
Created August 27, 2016 18:25
Show Gist options
  • Save vitqst/86d12d86260f77746e97e2605a2b25b3 to your computer and use it in GitHub Desktop.
Save vitqst/86d12d86260f77746e97e2605a2b25b3 to your computer and use it in GitHub Desktop.
package com.the360lifechange.service;
/**
* Created by thanhcs94 on 8/1/2016.
*/
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;
import com.the360lifechange.MainActivity;
import com.the360lifechange.R;
/**
* Created by Belal on 5/27/2016.
*/
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
try{
customLog("From: " + remoteMessage.getFrom());
customLog("Notification Msg title: " + remoteMessage.getNotification().getTitle());
customLog("Notification Msg body: " + remoteMessage.getNotification().getBody());
customLog("mess" + remoteMessage.getData().toString());
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}catch (Exception e){
customLog(e+"");
}
// Calling method to generate notification
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String title,String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
customLog("sendNotification : " + title);
customLog("sendNotification : " + messageBody);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
private void customLog(String log){
Log.e(TAG,"=======================") ;
Log.e(TAG,log) ;
Log.e(TAG,"=======================") ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment