本文整理汇总了Java中android.app.NotificationManager类的典型用法代码示例。如果您正苦于以下问题:Java NotificationManager类的具体用法?Java NotificationManager怎么用?Java NotificationManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NotificationManager类属于android.app包,在下文中一共展示了NotificationManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendNotification
import android.app.NotificationManager; //导入依赖的package包/类
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, QuakeActivity.class);//**The activity that you want to open when the notification is clicked
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_error_outline_white_24dp)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
示例2: createNotificationChannel
import android.app.NotificationManager; //导入依赖的package包/类
@TargetApi(26)
private void createNotificationChannel() {
notificationChannelClass = new NotificationChannel("class", "Class Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannelClass.setDescription("Notifications about classes.");
notificationChannelClass.enableLights(false);
notificationChannelClass.enableVibration(false);
notificationChannelClass.setBypassDnd(false);
notificationChannelClass.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannelClass.setShowBadge(false);
notificationManager.createNotificationChannel(notificationChannelClass);
notificationChannelReminder = new NotificationChannel("reminder", "Reminders", NotificationManager.IMPORTANCE_MAX);
notificationChannelReminder.setDescription("Notifications about events.");
notificationChannelReminder.enableLights(true);
notificationChannelReminder.setLightColor(sharedPreferences.getInt("primary_color", ContextCompat.getColor(this, R.color.teal)));
notificationChannelReminder.enableVibration(true);
notificationChannelReminder.setBypassDnd(true);
notificationChannelReminder.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannelReminder.setShowBadge(true);
notificationManager.createNotificationChannel(notificationChannelReminder);
}
示例3: startPhoneService
import android.app.NotificationManager; //导入依赖的package包/类
/**
* Starts the service that controls the user actions performed on the phone
* @param activity main game activity
*/
public static void startPhoneService(MainActivity activity) {
isLocked = false;
//the filters are the actions from the phone that we want to keep informed of
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
filter.addAction(Intent.ACTION_SCREEN_OFF);
//must register a receiver in order to do things when filter actions are registered
activity.mainRegisterReceiver(new UnlockReceiver(), filter); // TODO: Don't forget to unregister during onDestroy
PhoneService.mainActivity = activity;
mNotificationManager =
(NotificationManager) mainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
}
示例4: showNotification
import android.app.NotificationManager; //导入依赖的package包/类
private void showNotification(EMMessage emMessage) {
String contentText = "";
if (emMessage.getBody() instanceof EMTextMessageBody) {
contentText = ((EMTextMessageBody) emMessage.getBody()).getMessage();
}
Intent chat = new Intent(this, ChatActivity.class);
chat.putExtra(Constant.Extra.USER_NAME, emMessage.getUserName());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, chat, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.avatar1))
.setSmallIcon(R.mipmap.ic_contact_selected_2)
.setContentTitle(getString(R.string.receive_new_message))
.setContentText(contentText)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(1, notification);
}
示例5: onCreate
import android.app.NotificationManager; //导入依赖的package包/类
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationMap = new SparseIntArray();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
//mServiceHandler = new ServiceHandler(mServiceLooper);
mServiceHandler = new ServiceHandler(mServiceLooper, this);
setPrefs(PreferenceManager.getDefaultSharedPreferences(this));
}
示例6: notifyImpl
import android.app.NotificationManager; //导入依赖的package包/类
private void notifyImpl(Context context, OwnerInfo info){
String ownerName = fromId > 0 ? (stringEmptyIfNull(firstName) + " " + stringEmptyIfNull(lastName)) : groupName;
final NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Utils.hasOreo()){
nManager.createNotificationChannel(AppNotificationChannels.getNewPostChannel(context));
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AppNotificationChannels.NEW_POST_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notify_statusbar)
.setLargeIcon(info.getAvatar())
.setContentTitle(context.getString(R.string.new_post_title))
.setContentText(context.getString(R.string.new_post_was_published_in, ownerName))
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(Extra.PLACE, PlaceFactory.getPostPreviewPlace(accountId, postId, fromId));
intent.setAction(MainActivity.ACTION_OPEN_PLACE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, fromId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent);
Notification notification = builder.build();
configOtherPushNotification(notification);
nManager.notify(String.valueOf(fromId), NotificationHelper.NOTIFICATION_NEW_POSTS_ID, notification);
}
示例7: createNotification
import android.app.NotificationManager; //导入依赖的package包/类
private void createNotification(String message, String title) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSmallIcon(R.drawable.cake) // TODO: change icon
.setSound(defaultSoundUri);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
notificationBuilder.setColor(0xff023876);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
示例8: show
import android.app.NotificationManager; //导入依赖的package包/类
public static void show(Context context) {
// Build toggle action
Intent notificationServiceIntent = new Intent(context, NotificationService.class);
notificationServiceIntent.setAction(ACTION_TOGGLE);
PendingIntent notificationPendingIntent = PendingIntent.getService(context, 0, notificationServiceIntent, 0);
NotificationCompat.Action toggleAction = new NotificationCompat.Action(
R.drawable.ic_border_clear_black_24dp,
"Toggle",
notificationPendingIntent
);
// Build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_zoom_out_map_black_24dp)
.setContentTitle("Immersify")
.setContentText("Tap to toggle immersive mode")
.setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.addAction(toggleAction)
.setOngoing(true);
// Clear existing notifications
ToggleNotification.cancel(context);
// Notify the notification manager to create the notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
示例9: updateNotification
import android.app.NotificationManager; //导入依赖的package包/类
private void updateNotification() {
// Create a notification builder that's compatible with platforms >= version 4
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext());
// Set the title, text, and icon
builder.setContentTitle(getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_step_icon);
builder.setContentText("steps: " + StepsTaken.getSteps());
// Get an instance of the Notification Manager
NotificationManager notifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Build the notification and post it
notifyManager.notify(0, builder.build());
}
示例10: dismissKeyguardView
import android.app.NotificationManager; //导入依赖的package包/类
private void dismissKeyguardView () {
try {
mManager.removeView(mKeyguardView);
mContext.sendBroadcast(new Intent(KeyguardLiveActivity.ACTION_DISMISS));
Notification finishNotification = new NotificationCompat.Builder(mContext)
.setContentTitle(mContext.getString(R.string.notification_finish_title))
.setContentText(mContext.getString(R.string.notification_finish_text,
Utils.formatSecToStr(BigDecimal.valueOf(mUsedTime)),
String.valueOf(mScreenOn)))
.setSmallIcon(R.drawable.ic_stat_lock_open)
.setVibrate(new long[]{300})
.build();
((NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(0, finishNotification);
} catch (Exception e) {
e.printStackTrace();
android.os.Process.killProcess(
android.os.Process.myPid()
);
}
}
示例11: showProgressNotification
import android.app.NotificationManager; //导入依赖的package包/类
/**
* Show notification with a progress bar.
*/
protected void showProgressNotification(String caption, long completedUnits, long totalUnits) {
int percentComplete = 0;
if (totalUnits > 0) { percentComplete = (int) (100 * completedUnits / totalUnits); }
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
.setContentTitle(getString(R.string.godot_project_name_string))
.setContentText(caption)
.setProgress(100, percentComplete, false)
.setOngoing(true)
.setAutoCancel(false);
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(PROGRESS_NOTIFICATION_ID, builder.build());
}
示例12: addNotification
import android.app.NotificationManager; //导入依赖的package包/类
private void addNotification(Context context, String title, String message) {
//获取通知管理器服务
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = createDisplayMessageIntent(context, message, Utils.getNotificationId());
//新建一个notification
Notification.Builder builder = new Notification.Builder(context)
.setTicker(message)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);
builder.setFullScreenIntent(pendingIntent, true);
//开始通知
notificationManager.notify(Utils.getNotificationId(), builder.getNotification());
}
示例13: cancelActiveNotifications
import android.app.NotificationManager; //导入依赖的package包/类
private static void cancelActiveNotifications(@NonNull Context context) {
NotificationManager notifications = ServiceUtil.getNotificationManager(context);
notifications.cancel(SUMMARY_NOTIFICATION_ID);
if (Build.VERSION.SDK_INT >= 23) {
try {
StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();
for (StatusBarNotification activeNotification : activeNotifications) {
if (activeNotification.getId() != CallNotificationBuilder.WEBRTC_NOTIFICATION) {
notifications.cancel(activeNotification.getId());
}
}
} catch (Throwable e) {
// XXX Appears to be a ROM bug, see #6043
Log.w(TAG, e);
notifications.cancelAll();
}
}
}
示例14: handleIntent
import android.app.NotificationManager; //导入依赖的package包/类
private void handleIntent(Intent i) {
StartResult result = (StartResult) i.getSerializableExtra("briar.START_RESULT");
int notificationId = i.getIntExtra("briar.FAILURE_NOTIFICATION_ID", -1);
// cancel notification
if (notificationId > -1) {
Object o = getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.cancel(notificationId);
}
// show proper error message
TextView view = (TextView) findViewById(R.id.errorView);
if (result.equals(StartResult.DB_ERROR)) {
view.setText(getText(R.string.startup_failed_db_error));
} else if (result.equals(StartResult.SERVICE_ERROR)) {
view.setText(getText(R.string.startup_failed_service_error));
}
}
示例15: showUpdateNotAvailableNotification
import android.app.NotificationManager; //导入依赖的package包/类
static void showUpdateNotAvailableNotification(Context context, String title, String content, int smallIconResourceId) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, context.getPackageManager().getLaunchIntentForPackage(UtilsLibrary.getAppPackageName(context)), PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(content)
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setSmallIcon(smallIconResourceId)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setOnlyAlertOnce(true)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}