Created
April 15, 2017 08:05
-
-
Save thatisusama/30bb0470b3fbe306acdd694699c3d723 to your computer and use it in GitHub Desktop.
Retrieving Firebase Data Message in Android Notification
This file contains hidden or 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
/** | |
* Created by thatisusama on 20/3/2015. | |
*/ | |
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
String var1 = remoteMessage.getData().get("key1"); | |
String var2 = remoteMessage.getData().get("key2"); | |
GetNotification(message, postid); | |
} | |
public void GetNotification(final String var1,final String var2) | |
{ | |
NotificationCompat.Builder bd = new NotificationCompat.Builder(MyFirebaseMessagingService.this); | |
//bd.setContentIntent(pendingIntent); | |
bd.setLights(Color.BLUE, 1000, 1000); | |
bd.setSmallIcon(R.mipmap.ic_launcher); | |
bd.setContentTitle("Test_Title"); | |
bd.setContentText(var1); | |
// this intent will open your desired activity on notification click | |
Intent notificationIntent = new Intent(this, MainActivity.class); | |
notificationIntent.putExtra("YourDesiredName", var2); | |
notificationIntent.addFlags(notificationIntent.FLAG_ACTIVITY_NEW_TASK); | |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
bd.setContentIntent(contentIntent); | |
bd.setAutoCancel(true); | |
int notificationId = (int) (System.currentTimeMillis()%10000);//this id will be the time to avoid duplicate ids for notifications | |
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
nm.notify(notificationId,bd.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment