Forked from kristopherjohnson/showForegroundNotification.java
Created
August 15, 2016 01:11
-
-
Save tonyho/fd13ad06ea9f3283198eb72c41c1bd4d to your computer and use it in GitHub Desktop.
Shows a foreground notification for an Android service. Tapping the notification will display the app as if it was tapped in application launcher
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
private static final int NOTIFICATION_ID = 1; | |
private void showForegroundNotification(String contentText) { | |
// Create intent that will bring our app to the front, as if it was tapped in the app | |
// launcher | |
Intent showTaskIntent = new Intent(getApplicationContext(), MyMainActivity.class); | |
showTaskIntent.setAction(Intent.ACTION_MAIN); | |
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER); | |
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
PendingIntent contentIntent = PendingIntent.getActivity( | |
getApplicationContext(), | |
0, | |
showTaskIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
Notification notification = new Notification.Builder(getApplicationContext()) | |
.setContentTitle(getString(R.string.app_name)) | |
.setContentText(contentText) | |
.setSmallIcon(R.drawable.ic_notification) | |
.setWhen(System.currentTimeMillis()) | |
.setContentIntent(contentIntent) | |
.build(); | |
startForeground(NOTIFICATION_ID, notification); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment