Skip to content

Instantly share code, notes, and snippets.

@muratcanbur
Last active November 7, 2019 00:02
Show Gist options
  • Save muratcanbur/aa9b934454fbf4020d713de6e9d1f32f to your computer and use it in GitHub Desktop.
Save muratcanbur/aa9b934454fbf4020d713de6e9d1f32f to your computer and use it in GitHub Desktop.
a background service that handles incoming FCM messages.
package co.mobiwise.firebasetraining.service;
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 co.mobiwise.firebasetraining.MainActivity;
import co.mobiwise.firebasetraining.R;
public class MessagingService extends FirebaseMessagingService {
private static final String TAG = "MessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle data payload of FCM messages.
handleNotification(remoteMessage);
}
private void handleNotification(RemoteMessage remoteMessage) {
// Create Notification
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.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
@takieddine12
Copy link

i'm actually getting some data from sport api , four values : time,date , hometeam,awayteam , i want to post this data to https://fcm.googleapis.com/fcm/send , then retreive the posted data and put it in my notification , is there a good way to do that , thank you .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment