下面列出了android.app.NotificationManager#IMPORTANCE_DEFAULT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void init(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = "Library imports";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(ID, name, importance);
channel.setSound(null, null);
channel.setVibrationPattern(null);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
// Mandatory; it is not possible to change the sound of an existing channel after its initial creation
Objects.requireNonNull(notificationManager, "notificationManager must not be null");
notificationManager.deleteNotificationChannel(ID_OLD);
notificationManager.createNotificationChannel(channel);
}
}
public static void init(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = "Book downloads";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(ID, name, importance);
channel.setSound(null, null);
channel.setVibrationPattern(null);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
// Mandatory; it is not possible to change the sound of an existing channel after its initial creation
Objects.requireNonNull(notificationManager, "notificationManager must not be null");
notificationManager.deleteNotificationChannel(ID_OLD);
notificationManager.createNotificationChannel(channel);
}
}
/**
* Creates the notification channel
* Which allows and manages the displaying of the notification
*/
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
mNotificationManager = getSystemService(NotificationManager.class);
mNotificationManager.createNotificationChannel(channel);
}
}
private static void checkOrCreateChannel(NotificationManager manager) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
if (channelCreated)
return;
if (manager == null)
return;
final CharSequence name = "Notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
channel.enableLights(true);
channel.enableVibration(true);
manager.createNotificationChannel(channel);
channelCreated = true;
}
private void createNotifyChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannelGroup(
new NotificationChannelGroup(mGroupId, mGroupName));
NotificationChannel channelMsg =
new NotificationChannel(mChannelMsgId, mChannelMsgName,
NotificationManager.IMPORTANCE_DEFAULT);
channelMsg.setDescription(mChannelMsgDes);
channelMsg.enableLights(true);
channelMsg.setLightColor(Color.BLUE);
channelMsg.enableVibration(false);
channelMsg.setVibrationPattern(new long[] { 100, 200, 300, 400 });
channelMsg.setShowBadge(true);
channelMsg.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channelMsg.setGroup(mGroupId);
mNotificationManager.createNotificationChannel(channelMsg);
NotificationChannel channelService =
new NotificationChannel(mChannelServiceId, mChannelServiceName,
NotificationManager.IMPORTANCE_LOW);
channelService.setDescription(mChannelServiceDes);
channelService.enableLights(false);
channelService.enableVibration(false);
channelService.setShowBadge(false);
channelService.setSound(null, null);
channelService.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channelService.setGroup(mGroupId);
mNotificationManager.createNotificationChannel(channelService);
}
}
}
public static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if (notificationManager != null) {
NotificationChannel channel
= new NotificationChannel(context.getPackageName(), context.getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
}
}
@TargetApi(26)
private static NotificationChannel createChannelWithPackage(@NonNull String packageName,
@NonNull CharSequence name) {
NotificationChannel channel = new NotificationChannel(getChannelIdByPkg(packageName),
name, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(true);
return channel;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
LocationResultHelper(Context context, List<Location> locations) {
mContext = context;
mLocations = locations;
NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL,
context.getString(R.string.default_channel), NotificationManager.IMPORTANCE_DEFAULT);
channel.setLightColor(Color.GREEN);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getNotificationManager().createNotificationChannel(channel);
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(NotificationManager notificationManager) {
NotificationChannel channel = new NotificationChannel(
OTHER_NOTIFICATION_CHANNEL_ID,
context.getString(R.string.other_notification_channel_name),
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
public static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
String name = context.getString(R.string.channel_blocked_messages);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, name, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
@Override
public void onCreate() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
onNewLocation(locationResult.getLastLocation());
}
};
createLocationRequest();
getLastLocation();
HandlerThread handlerThread = new HandlerThread(TAG);
handlerThread.start();
mServiceHandler = new Handler(handlerThread.getLooper());
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Android O requires a Notification Channel.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
// Create the channel for the notification
NotificationChannel mChannel =
new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
// Set the Notification Channel for the Notification Manager.
mNotificationManager.createNotificationChannel(mChannel);
}
}
private static void createNotificationChannel(NotificationManager notificationManager, Context context) {
if (Build.VERSION.SDK_INT >= 26) {
CharSequence name = context.getString(R.string.notification_crash_report_title);
String description = "";
NotificationChannel channel = new NotificationChannel(CHANNEL_NOTIFICATION_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
}
@TargetApi(26)
public static NotificationChannel getChan(Notification.Builder wip) {
final Notification temp = wip.build();
if (temp.getChannelId() == null) return null;
// create generic audio attributes
final AudioAttributes generic_audio = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();
// create notification channel for hashing purposes from the existing notification builder
NotificationChannel template = new NotificationChannel(
temp.getChannelId(),
getString(temp.getChannelId()),
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the notification parameters in the channel
template.setGroup(temp.getChannelId());
template.setVibrationPattern(temp.vibrate);
template.setSound(temp.sound, generic_audio);
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// get a nice string to identify the hash
final String mhash = my_text_hash(template);
// create another notification channel using the hash because id is immutable
final NotificationChannel channel = new NotificationChannel(
template.getId() + mhash,
getString(temp.getChannelId()) + mhash,
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// create a group to hold this channel if one doesn't exist or update text
getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
// create this channel if it doesn't exist or update text
getNotifManager().createNotificationChannel(channel);
return channel;
}
private void CreateNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Channel 1 Configuration
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID,
"channel 1",NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("Test Channel 1");
//Channel 2 Configuration
NotificationChannel channel2 = new NotificationChannel(CHANNEL_2_ID,
"channel 2",NotificationManager.IMPORTANCE_LOW);
channel2.setDescription("Test Channel 2");
//Channel 3 Configuration
NotificationChannel channel3 = new NotificationChannel(CHANNEL_3_ID,
"channel 3",NotificationManager.IMPORTANCE_DEFAULT);
channel3.setDescription("Test Channel 3");
//Channel 4 Configuration
NotificationChannel channel4 = new NotificationChannel(CHANNEL_4_ID,
"channel 4",NotificationManager.IMPORTANCE_HIGH);
channel4.setDescription("Test Channel 4");
//Channel 5 Configuration
NotificationChannel channel5 = new NotificationChannel(CHANNEL_5_ID,
"channel 5",NotificationManager.IMPORTANCE_HIGH);
channel5.setDescription("Test Channel 5");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
manager.createNotificationChannel(channel3);
manager.createNotificationChannel(channel4);
manager.createNotificationChannel(channel5);
}
}
private void notify(CallInvite callInvite, int notificationId) {
String callSid = callInvite.getCallSid();
Notification notification = null;
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
intent.setAction(TwilioVoicePlugin.ACTION_INCOMING_CALL);
intent.putExtra(TwilioVoicePlugin.INCOMING_CALL_NOTIFICATION_ID, notificationId);
intent.putExtra(TwilioVoicePlugin.INCOMING_CALL_INVITE, callInvite);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Bundle extras = new Bundle();
extras.putInt(NOTIFICATION_ID_KEY, notificationId);
extras.putString(CALL_SID_KEY, callSid);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel callInviteChannel = new NotificationChannel(VOICE_CHANNEL,
"Primary Voice Channel", NotificationManager.IMPORTANCE_DEFAULT);
callInviteChannel.setLightColor(Color.RED);
callInviteChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(callInviteChannel);
notification = buildNotification(callInvite.getFrom() + " is calling", pendingIntent, extras);
notificationManager.notify(notificationId, notification);
} else {
int iconIdentifier = getResources().getIdentifier("icon", "mipmap", getPackageName());
int incomingCallAppNameId = (int) getResources().getIdentifier("incoming_call_app_name", "string", getPackageName());
String contentTitle = getString(incomingCallAppNameId);
if (contentTitle == null) {
contentTitle = "Incoming Call";
}
final String from = callInvite.getFrom() + " is calling";
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(iconIdentifier)
.setContentTitle(contentTitle)
.setContentText(from)
.setAutoCancel(true)
.setExtras(extras)
.setContentIntent(pendingIntent)
.setGroup("voice_app_notification")
.setColor(Color.rgb(225, 0, 0));
notificationManager.notify(notificationId, notificationBuilder.build());
}
}
public void sendNotification(NotificationEvent event, Bitmap avatar) {
eventsHistory.put(event.notifyId(), event);
String title = createTitle(event);
String text = createContent(event);
String summaryText = createSummary(event);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(text);
bigTextStyle.setSummaryText(summaryText);
String channelId = getChannelId(event);
String channelName = getChannelName(event);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
if (avatar != null && !event.fromSite()) {
builder.setLargeIcon(avatar);
}
builder.setSmallIcon(createSmallIcon(event));
builder.setContentTitle(title);
builder.setContentText(text);
builder.setStyle(bigTextStyle);
builder.setChannelId(channelId);
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setData(Uri.parse(createIntentUrl(event)));
notifyIntent.setAction(Intent.ACTION_VIEW);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
builder.setContentIntent(notifyPendingIntent);
configureNotification(builder);
mNotificationManager.cancel(event.notifyId());
mNotificationManager.notify(event.notifyId(), builder.build());
}
@TargetApi(Build.VERSION_CODES.O)
private NotificationChannel getNotificationChannel() {
return new NotificationChannel(PACKAGE_NAME, NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private Notification.Builder getNewStyleNotification(int smallIcon, Bitmap largeIcon, String title, String text, int priority, Intent resultIntent, int notifType, String soundURI, long[] vibrationPattern) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.R_string_notif_channel_name);
String description = getString(R.string.notif_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Notification.Builder mBuilder =
new Notification.Builder(getApplicationContext())
.setSmallIcon(smallIcon)
.setLargeIcon(largeIcon)
.setContentTitle(title)
.setContentText(text)
.setPriority(priority)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setChannelId(CHANNEL_ID);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setCategory(Notification.CATEGORY_SOCIAL)
.setVisibility(Notification.VISIBILITY_PRIVATE);
}
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
if (soundURI != null) {
Uri uri = Uri.parse(soundURI);
mBuilder.setSound(uri);
}
mBuilder.setVibrate(vibrationPattern);
return mBuilder;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(onPageChangeListener);
fabAdd = findViewById(R.id.fab);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, KeyEditActivity.class);
startActivityForResult(intent, KEY_EDIT_REQUEST);
}
});
// attempt extraction in activity, to make service start faster
StunnelProcessManager.checkAndExtract(this);
StunnelProcessManager.setupConfig(this);
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.notification_channel);
String description = getString(R.string.notification_desc);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}