-
-
Save stepenik/de37d985f882c3adb562d2417276ca32 to your computer and use it in GitHub Desktop.
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
| public class NotificationHelper { | |
| private Context mContext; | |
| private NotificationManager mNotificationManager; | |
| private NotificationCompat.Builder mBuilder; | |
| public static final String NOTIFICATION_CHANNEL_ID = "10001"; | |
| public NotificationHelper(Context context) { | |
| mContext = context; | |
| } | |
| /** | |
| * Create and push the notification | |
| */ | |
| public void createNotification(String title, String message) | |
| { | |
| /**Creates an explicit intent for an Activity in your app**/ | |
| Intent resultIntent = new Intent(mContext , SomeOtherActivity.class); | |
| resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, | |
| 0 /* Request code */, resultIntent, | |
| PendingIntent.FLAG_UPDATE_CURRENT); | |
| mBuilder = new NotificationCompat.Builder(mContext); | |
| mBuilder.setSmallIcon(R.mipmap.ic_launcher); | |
| mBuilder.setContentTitle(title) | |
| .setContentText(message) | |
| .setAutoCancel(false) | |
| .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) | |
| .setContentIntent(resultPendingIntent); | |
| mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); | |
| if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) | |
| { | |
| int importance = NotificationManager.IMPORTANCE_HIGH; | |
| NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance); | |
| notificationChannel.enableLights(true); | |
| notificationChannel.setLightColor(Color.RED); | |
| notificationChannel.enableVibration(true); | |
| notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); | |
| assert mNotificationManager != null; | |
| mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID); | |
| mNotificationManager.createNotificationChannel(notificationChannel); | |
| } | |
| assert mNotificationManager != null; | |
| mNotificationManager.notify(0 /* Request Code */, mBuilder.build()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment