Created
February 28, 2021 17:23
-
-
Save pedromaironi/17adef904b93e3f03364794518f7f87c 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
private void showNotification(String uri, String title, String body) { | |
NotificationHelper notificationHelper = new NotificationHelper(getBaseContext()); | |
NotificationCompat.Builder builder = notificationHelper.getNotification(uri,title, body); | |
Random random = new Random(); | |
int n = random.nextInt(10000); | |
notificationHelper.getManager().notify(n, builder.build()); | |
} | |
// Class | |
package com.pedrodev.check_version.utils; | |
import android.app.DownloadManager; | |
import android.app.Notification; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.content.Intent; | |
import android.graphics.Color; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.provider.DocumentsContract; | |
import androidx.annotation.RequiresApi; | |
import androidx.core.app.NotificationCompat; | |
import com.pedrodev.check_version.R; | |
public class NotificationHelper extends ContextWrapper { | |
private static final String CHANNEL_ID = "com.pedrodev.Checkversion"; | |
private static final String CHANNEL_NAME = "SYSnotes"; | |
private NotificationManager manager; | |
public NotificationHelper(Context context) { | |
super(context); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
createChannels(); | |
} | |
} | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
private void createChannels() { | |
NotificationChannel notificationChannel = new NotificationChannel( | |
CHANNEL_ID, | |
CHANNEL_NAME, | |
NotificationManager.IMPORTANCE_HIGH | |
); | |
notificationChannel.enableLights(true); | |
notificationChannel.enableVibration(true); | |
notificationChannel.setLightColor(Color.GRAY); | |
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); | |
getManager().createNotificationChannel(notificationChannel); | |
} | |
public NotificationManager getManager() { | |
if (manager == null) { | |
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
} | |
return manager; | |
} | |
public NotificationCompat.Builder getNotification(String uri, String title, String body) { | |
return new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) | |
.setContentTitle(title) | |
.setContentText(body) | |
.setColor(Color.GRAY) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setAutoCancel(true) | |
.setStyle(new NotificationCompat.BigTextStyle().bigText(body).setBigContentTitle(title)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment