Last active
August 5, 2019 17:39
-
-
Save raykibul/16a08ada7f2c5d311ad5a3bed3f878d1 to your computer and use it in GitHub Desktop.
android Create 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
| import android.app.NotificationManager; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.widget.Toast; | |
| import androidx.core.app.NotificationCompat; | |
| import static android.content.Context.NOTIFICATION_SERVICE; | |
| import static androidx.core.content.ContextCompat.getSystemService; | |
| public class MyBroadCastReciver extends BroadcastReceiver { | |
| private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel"; | |
| private NotificationManager mNotifyManager; | |
| private NotificationHelper notificationHelper; | |
| @Override | |
| public void onReceive(Context context, Intent intent) { | |
| notificationHelper=new NotificationHelper(context); | |
| NotificationCompat.Builder nb= notificationHelper.getChannelNotification("ATTENTION!!!","ONE OF YOUR PET MEDICINE WIll FINISH TOMORROW"); | |
| notificationHelper.getManager().notify(1,nb.build()); | |
| Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show(); | |
| } | |
| } |
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
| import android.annotation.TargetApi; | |
| import android.app.Notification; | |
| import android.app.NotificationChannel; | |
| import android.app.NotificationManager; | |
| import android.app.PendingIntent; | |
| import android.content.Context; | |
| import android.content.ContextWrapper; | |
| import android.content.Intent; | |
| import android.os.Build; | |
| import androidx.core.app.NotificationCompat; | |
| public class NotificationHelper extends ContextWrapper { | |
| public static final String channelId="ELANCO_NOTIFICATION"; | |
| public static final String channelName="ELANCO_NOTIFICATION_CHANNEL"; | |
| private NotificationManager manager; | |
| public NotificationHelper(Context base) { | |
| super(base); | |
| if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ | |
| CreateChannel(); | |
| } | |
| } | |
| @TargetApi(Build.VERSION_CODES.O) | |
| private void CreateChannel() { | |
| NotificationChannel channel=new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_DEFAULT); | |
| channel.enableVibration(true); | |
| channel.enableLights(true); | |
| channel.setLightColor(R.color.colorPrimary); | |
| channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); | |
| getManager().createNotificationChannel(channel); | |
| } | |
| public NotificationManager getManager(){ | |
| if (manager==null){ | |
| manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); | |
| } | |
| return manager; | |
| } | |
| public NotificationCompat.Builder getChannelNotification(String title,String message){ | |
| Intent myinten=new Intent(this,MainActivity.class); | |
| PendingIntent pendingIntent=PendingIntent.getActivity(this,2,myinten,PendingIntent.FLAG_UPDATE_CURRENT); | |
| return new NotificationCompat.Builder(getApplicationContext(),channelId) | |
| .setContentTitle(title) | |
| .setContentText(message) | |
| .setSmallIcon(R.drawable.ic_notificaion) | |
| .setAutoCancel(true) | |
| .setContentIntent(pendingIntent) | |
| ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment