Created
January 19, 2021 19:06
-
-
Save AbdelrahmanElShikh/6bd2b0efab30fbc2f00715c0088f867c to your computer and use it in GitHub Desktop.
This file contains 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
package <Package name> | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Build; | |
import androidx.core.app.NotificationCompat; | |
import androidx.core.content.ContextCompat; | |
/** | |
* @author Abdel-Rahman El-Shikh | |
*/ | |
public class NotificationUtils { | |
private static final String CHANNEL_ID = "message_notification_channel"; | |
private static final int MESSAGE_NOTIFICATION_ID = 1138; | |
private static final int MESSAGE_PENDING_INTENT_ID = 3471; | |
public static void notifyUser(Context context, String title, String body) { | |
NotificationManager notificationManager = (NotificationManager) | |
context.getSystemService(Context.NOTIFICATION_SERVICE); | |
/** | |
* This if statement block is so important as it creates notification channel for android devices above android Oreo. | |
* and we can't send notification to these devices without this channel and any other device (below android Oreo) | |
* will ignore the channel parameter.(Abdel-Rahman) | |
*/ | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
NotificationChannel mChannel = new NotificationChannel( | |
CHANNEL_ID, | |
context.getString(R.string.main_notification_channel_name), | |
NotificationManager.IMPORTANCE_HIGH); | |
assert notificationManager != null; | |
notificationManager.createNotificationChannel(mChannel); | |
} | |
/** | |
* Here we are building the notification and setting it's content | |
* PARAM -> (CHANNEL_ID) Will be ignored with devices below Android Oreo | |
*/ | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID) | |
.setColor(ContextCompat.getColor(context, R.color.colorPrimary)) | |
.setSmallIcon() | |
.setContentTitle(title) | |
.setContentText(body) | |
.setStyle(new NotificationCompat.BigTextStyle().bigText(body)) | |
.setDefaults(NotificationCompat.DEFAULT_LIGHTS) | |
.setContentIntent(contentIntent(context)) | |
.setAutoCancel(true) | |
.setPriority(NotificationCompat.PRIORITY_DEFAULT); | |
assert notificationManager != null; | |
notificationManager.notify(MESSAGE_NOTIFICATION_ID, notificationBuilder.build()); | |
} | |
private static PendingIntent contentIntent(Context context) { | |
Intent intent = new Intent(context, <YourActivity>.class); | |
return PendingIntent.getActivity(context, MESSAGE_PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment