下面列出了android.app.Notification.Action#android.app.Notification.Builder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Populates remote views for pre-N targeting apps.
*/
private void maybePopulateRemoteViews(Notification notification) {
if (getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
Builder builder = Builder.recoverBuilder(getContext(), notification);
// Some styles wrap Notification's contentView, bigContentView and headsUpContentView.
// First inflate them all, only then set them to avoid recursive wrapping.
RemoteViews content = builder.createContentView();
RemoteViews big = builder.createBigContentView();
RemoteViews headsUp = builder.createHeadsUpContentView();
notification.contentView = content;
notification.bigContentView = big;
notification.headsUpContentView = headsUp;
}
}
private void setOnClick(Builder builder, JSONObject dataToReturnOnClick) {
Intent intent = new Intent(this.context, NotificationActivity.class).setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Iterator<?> keys = dataToReturnOnClick.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
Object value;
try {
value = dataToReturnOnClick.get(key);
} catch(JSONException e){
value = null;
}
intent.putExtra(key, value == null ? null : value.toString());
}
int reqCode = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(
this.context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
}
public static void init(Service a_service, Context a_context)
{
service = a_service;
context = a_context;
note_manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pending_click = null;
String intent_click = context.getString(R.string.intent_click);
log("Notify: using broadcasts to deliver clicks.");
Intent click = new Intent(intent_click);
pending_click = PendingIntent.getBroadcast(context, 0, click, 0);
builder =
new Notification.Builder(context)
.setOngoing(false)
.setSmallIcon(R.drawable.intent_radio)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pending_click)
.setContentTitle(service.getString(R.string.app_name_long))
;
}
/**
* @hide
*/
public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
{
INotificationManager service = getService();
String pkg = mContext.getPackageName();
// Fix the notification as best we can.
Notification.addFieldsFromContext(mContext, notification);
if (notification.sound != null) {
notification.sound = notification.sound.getCanonicalUri();
if (StrictMode.vmFileUriExposureEnabled()) {
notification.sound.checkFileUriExposed("Notification.sound");
}
}
fixLegacySmallIcon(notification, pkg);
if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (notification.getSmallIcon() == null) {
throw new IllegalArgumentException("Invalid notification (no valid small icon): "
+ notification);
}
}
if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
notification.reduceImageSizes(mContext);
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
boolean isLowRam = am.isLowRamDevice();
final Notification copy = Builder.maybeCloneStrippedForDelivery(notification, isLowRam,
mContext);
try {
service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
copy, user.getIdentifier());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
private void showDebugNotification(final Conversation convs, final @Nullable String summary) {
final StringBuilder bigText = new StringBuilder().append(convs.summary).append("\nT:").append(convs.ticker);
final String[] messages = convs.ext != null ? convs.ext.getMessages() : null;
if (messages != null) for (final String msg : messages) bigText.append("\n").append(msg);
final Builder n = new Builder(mContext).setSmallIcon(android.R.drawable.stat_sys_warning)
.setContentTitle(convs.key).setContentText(convs.ticker).setSubText(summary).setShowWhen(true)
.setStyle(new BigTextStyle().setBigContentTitle(convs.title).bigText(bigText.toString()));
if (SDK_INT >= O) n.setChannelId("Debug");
requireNonNull(mContext.getSystemService(NotificationManager.class))
.notify(convs.key != null ? convs.key.hashCode() : convs.title.hashCode(), n.build());
}
@TargetApi(16)
final Notification a(Builder builder) {
builder.setDefaults(this.notificationDefaults);
builder.setSmallIcon(this.statusBarDrawable);
Notification build = builder.build();
build.flags = this.notificationFlags;
return build;
}
static /* synthetic */ void a(DownloadService downloadService, int i, c cVar, int i2) {
if (i2 != 0 && !cVar.e()) {
Object obj;
int i3 = 4;
if (2 == i2) {
obj = z[20];
} else if (3 == i2) {
obj = z[18];
} else if (1 == i2) {
String str = z[19];
i3 = 2;
} else {
return;
}
Object obj2 = cVar.s;
Intent intent = new Intent();
if (b.a(i2)) {
intent.setClass(downloadService.getApplicationContext(), DownloadService.class);
cVar.z = -1;
intent.putExtra(z[17], cVar);
}
PendingIntent service = PendingIntent.getService(downloadService, i, intent, 134217728);
if (VERSION.SDK_INT >= 11) {
new Builder(downloadService.getApplicationContext()).setContentTitle(obj2).setContentText(obj).setContentIntent(service).setWhen(System.currentTimeMillis()).setSmallIcon(17301634).getNotification().flags = i3;
} else {
Notification notification = new Notification();
notification.icon = 17301634;
notification.when = System.currentTimeMillis();
notification.flags = i3;
m.a(notification, downloadService.getApplicationContext(), obj2, obj, service);
}
if (downloadService.f != null) {
downloadService.c.notify(i, downloadService.f);
}
}
}
public static void b(Context context, String str, String str2, int i) {
Notification notification;
Intent intent = new Intent(context, PushReceiver.class);
intent.setAction(z[69]);
intent.putExtra(z[70], true);
intent.putExtra(z[96], str2);
intent.putExtra(z[11], i);
PendingIntent broadcast = PendingIntent.getBroadcast(context, 0, intent, 134217728);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(z[68]);
int i2 = -1;
try {
i2 = context.getPackageManager().getPackageInfo(context.getPackageName(), 256).applicationInfo.icon;
} catch (NameNotFoundException e) {
}
if (i2 < 0) {
i2 = 17301586;
}
Object obj = z[95];
Object obj2 = z[94];
long currentTimeMillis = System.currentTimeMillis();
if (VERSION.SDK_INT >= 11) {
notification = new Builder(context.getApplicationContext()).setContentTitle(obj).setContentText(obj2).setContentIntent(broadcast).setSmallIcon(i2).setTicker(str).setWhen(currentTimeMillis).getNotification();
notification.flags = 34;
} else {
Notification notification2 = new Notification(i2, str, currentTimeMillis);
notification2.flags = 34;
m.a(notification2, context, obj, obj2, broadcast);
notification = notification2;
}
if (notification != null) {
notificationManager.notify(str.hashCode(), notification);
}
}
public static void c(Context context, String str, String str2) {
if (g(context)) {
Notification notification;
z.b();
Intent intent = new Intent(context, PushReceiver.class);
intent.setAction(z[69]);
intent.putExtra(z[70], true);
intent.putExtra(z[71], str2);
PendingIntent broadcast = PendingIntent.getBroadcast(context, 0, intent, 134217728);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(z[68]);
int i = -1;
try {
i = context.getPackageManager().getPackageInfo(context.getPackageName(), 256).applicationInfo.icon;
} catch (NameNotFoundException e) {
}
if (i < 0) {
i = 17301586;
}
Object obj = z[67];
Object obj2 = z[66];
long currentTimeMillis = System.currentTimeMillis();
if (VERSION.SDK_INT >= 11) {
notification = new Builder(context.getApplicationContext()).setContentTitle(obj).setContentText(obj2).setContentIntent(broadcast).setSmallIcon(i).setTicker(str).setWhen(currentTimeMillis).getNotification();
notification.flags = 34;
} else {
Notification notification2 = new Notification(i, str, currentTimeMillis);
notification2.flags = 34;
m.a(notification2, context, obj, obj2, broadcast);
notification = notification2;
}
if (notification != null) {
notificationManager.notify(str.hashCode(), notification);
}
}
}
private void createNotificationChannel(Options options) {
// 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) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
if(options.doesHeadsUp()) {
importance = NotificationManager.IMPORTANCE_HIGH;
} else if(!options.doesSound()) {
importance = NotificationManager.IMPORTANCE_LOW;
}
NotificationChannel channel = new NotificationChannel(options.getChannelId(), options.getChannelName(), importance);
if(options.doesHeadsUp()) {
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}
channel.setDescription(options.getChannelDescription());
if(options.doesSound() && options.getSoundUri() != null) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setLegacyStreamType(android.media.AudioManager.STREAM_NOTIFICATION)
.build();
channel.setSound(options.getSoundUri(), audioAttributes);
}
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = this.context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@Override public void onReceive(final Context context, final Intent proxy) {
final PendingIntent reply_action = proxy.getParcelableExtra(EXTRA_REPLY_ACTION);
final String result_key = proxy.getStringExtra(EXTRA_RESULT_KEY), reply_prefix = proxy.getStringExtra(EXTRA_REPLY_PREFIX);
final Uri data = proxy.getData(); final Bundle results = RemoteInput.getResultsFromIntent(proxy);
final CharSequence input = results != null ? results.getCharSequence(result_key) : null;
if (data == null || reply_action == null || result_key == null || input == null) return; // Should never happen
final String key = data.getSchemeSpecificPart(), original_key = proxy.getStringExtra(EXTRA_ORIGINAL_KEY);
if (BuildConfig.DEBUG && "debug".equals(input.toString())) {
final Conversation conversation = mController.getConversation(proxy.getIntExtra(EXTRA_CONVERSATION_ID, 0));
if (conversation != null) showDebugNotification(conversation, "Type: " + conversation.typeToString());
mController.recastNotification(original_key != null ? original_key : key, null);
return;
}
final CharSequence text;
if (reply_prefix != null) {
text = reply_prefix + input;
results.putCharSequence(result_key, text);
RemoteInput.addResultsToIntent(new RemoteInput[]{ new RemoteInput.Builder(result_key).build() }, proxy, results);
} else text = input;
final ArrayList<CharSequence> history = SDK_INT >= N ? proxy.getCharSequenceArrayListExtra(EXTRA_REMOTE_INPUT_HISTORY) : null;
try {
final Intent input_data = addTargetPackageAndWakeUp(reply_action);
input_data.setClipData(proxy.getClipData());
reply_action.send(mContext, 0, input_data, (pendingIntent, intent, _result_code, _result_data, _result_extras) -> {
if (BuildConfig.DEBUG) Log.d(TAG, "Reply sent: " + intent.toUri(0));
if (SDK_INT >= N) {
final Bundle addition = new Bundle(); final CharSequence[] inputs;
final boolean to_current_user = Process.myUserHandle().equals(pendingIntent.getCreatorUserHandle());
if (to_current_user && context.getPackageManager().queryBroadcastReceivers(intent, 0).isEmpty()) {
inputs = new CharSequence[] { context.getString(R.string.wechat_with_no_reply_receiver) };
} else if (history != null) {
history.add(0, text);
inputs = history.toArray(new CharSequence[0]);
} else inputs = new CharSequence[] { text };
addition.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, inputs);
mController.recastNotification(original_key != null ? original_key : key, addition);
}
markRead(key);
}, null);
} catch (final PendingIntent.CanceledException e) {
Log.w(TAG, "Reply action is already cancelled: " + key);
abortBroadcast();
}
}
private static Person buildPersonFromProfile(final Context context) {
return new Person.Builder().setName(context.getString(R.string.self_display_name))
.setIcon(IconCompat.createWithContentUri(Uri.withAppendedPath(Profile.CONTENT_URI, Contacts.Photo.DISPLAY_PHOTO))).build();
}
@TargetApi(16)
Notification a(Builder builder) {
return builder.build();
}
private void a(c cVar, int i, long j, long j2) {
int i2;
if (VERSION.SDK_INT >= 11) {
if (this.g == null) {
this.g = new Builder(getApplicationContext()).setSmallIcon(17301633).setWhen(System.currentTimeMillis()).setDefaults(4).setOngoing(true);
this.g.setContentIntent(PendingIntent.getActivity(getApplicationContext(), i, new Intent(), 134217728));
}
CharSequence charSequence = cVar.s;
CharSequence charSequence2 = z[8];
i2 = (int) ((((float) j) / ((float) j2)) * 100.0f);
if (j2 > 0) {
charSequence2 = charSequence2 + i2 + "%";
}
if (this.m == null || this.m.intValue() <= 0) {
this.g.setContentTitle(charSequence).setContentText(charSequence2).setContentIntent(PendingIntent.getActivity(getApplicationContext(), i, new Intent(), 134217728));
} else {
if (this.h == null) {
this.h = new RemoteViews(getPackageName(), this.m.intValue());
this.h.setTextViewText(this.i.intValue(), charSequence);
this.h.setImageViewResource(this.k.intValue(), e.b);
}
this.h.setTextViewText(this.l.intValue(), i2 + "%");
this.h.setProgressBar(this.j.intValue(), 100, i2, false);
this.g.setContent(this.h);
}
this.c.notify(i, this.g.getNotification());
return;
}
if (this.f == null) {
this.f = new Notification();
this.f.icon = 17301633;
this.f.when = System.currentTimeMillis();
this.f.flags = 2;
this.f.defaults = 4;
this.f.contentIntent = PendingIntent.getActivity(getApplicationContext(), i, new Intent(), 134217728);
}
Object obj = cVar.s;
String str = z[8];
i2 = (int) ((((float) j) / ((float) j2)) * 100.0f);
if (j2 > 0) {
str = str + i2 + "%";
}
if (this.m == null || this.m.intValue() <= 0) {
m.a(this.f, this, obj, str, PendingIntent.getActivity(getApplicationContext(), i, new Intent(), 134217728));
} else {
if (this.h == null) {
this.h = new RemoteViews(getPackageName(), this.m.intValue());
this.h.setTextViewText(this.i.intValue(), obj);
this.h.setImageViewResource(this.k.intValue(), e.b);
}
this.h.setTextViewText(this.l.intValue(), i2 + "%");
this.h.setProgressBar(this.j.intValue(), 100, i2, false);
this.f.contentView = this.h;
}
if (this.f != null) {
this.c.notify(i, this.f);
}
}
public void showNotification(JSONObject dataToReturnOnClick, JSONObject notificationOptions){
Options options = new Options(notificationOptions, this.context.getApplicationContext());
this.createNotificationChannel(options);
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new Notification.Builder(this.context, options.getChannelId());
} else {
builder = new Notification.Builder(this.context);
}
builder.setDefaults(0)
.setContentTitle(options.getTitle()).setSmallIcon(options.getSmallIconResourceId())
.setLargeIcon(options.getLargeIconBitmap()).setAutoCancel(options.doesAutoCancel());
if(options.getBigPictureBitmap() != null)
builder.setStyle(new BigPictureStyle().bigPicture(options.getBigPictureBitmap()));
if(options.doesVibrate() && options.getVibratePattern() != null)
builder.setVibrate(options.getVibratePattern());
else if (Build.VERSION.SDK_INT >= 21 && options.doesHeadsUp())
builder.setVibrate(new long[0]);
if(options.doesHeadsUp()) {
builder.setPriority(Notification.PRIORITY_HIGH);
}
if(options.doesSound() && options.getSoundUri() != null)
builder.setSound(options.getSoundUri(), android.media.AudioManager.STREAM_NOTIFICATION);
if (options.doesColor() && Build.VERSION.SDK_INT >= 22)
builder.setColor(options.getColor());
this.setContentTextAndMultiline(builder, options);
this.setOnClick(builder, dataToReturnOnClick);
Notification notification;
if (Build.VERSION.SDK_INT < 16) {
notification = builder.getNotification(); // Notification for HoneyComb to ICS
} else {
notification = builder.build(); // Notification for Jellybean and above
}
if(options.doesAutoCancel())
notification.flags |= Notification.FLAG_AUTO_CANCEL;
if(options.doesVibrate() && options.getVibratePattern() == null)
notification.defaults |= Notification.DEFAULT_VIBRATE;
if(options.doesSound() && options.getSoundUri() == null)
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager
= (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(options.getId(), notification);
if(options.doesOpenApp())
openApp();
}