Java Code Examples for android.app.NotificationManager#notify()

The following examples show how to use android.app.NotificationManager#notify() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From -Android_ShareSDK_Example_Wechat with MIT License 6 votes vote down vote up
private void showNotification(long cancelTime, String text) {
	try {
		Context app = getApplicationContext();
		NotificationManager nm = (NotificationManager) app
				.getSystemService(Context.NOTIFICATION_SERVICE);
		final int id = Integer.MAX_VALUE / 13 + 1;
		nm.cancel(id);

		long when = System.currentTimeMillis();
		Notification notification = new Notification(R.drawable.ic_launcher, text, when);
		PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(), 0);
		notification.setLatestEventInfo(app, "sharesdk test", text, pi);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		nm.notify(id, notification);

		if (cancelTime > 0) {
			Message msg = new Message();
			msg.what = MSG_CANCEL_NOTIFY;
			msg.obj = nm;
			msg.arg1 = id;
			UIHandler.sendMessageDelayed(msg, cancelTime, this);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 2
Source File: NotificationManagerHelper.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
private static void sendChatNotificationEvent(Context context, Intent intent, NotificationEvent notificationEvent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannelIfNotExist(notificationManager);
    }

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
            .setSmallIcon(getNotificationIcon())
            .setContentTitle(notificationEvent.getTitle())
            .setColor(context.getResources().getColor(R.color.accent))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationEvent.getSubject()))
            .setContentText(notificationEvent.getBody())
            .setAutoCancel(true)
            .setContentIntent(contentIntent);

    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults = Notification.DEFAULT_ALL;
    notificationManager.notify(NOTIFICATION_ID, notification);
}
 
Example 3
Source File: SecondActivity.java    From AndroidAll with Apache License 2.0 6 votes vote down vote up
public void sendNotificationForBack2() {
    Intent backIntent = new Intent(this, MainActivity.class);
    backIntent.putExtra("from", "sendNotificationForBack2");
    backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Intent intent = new Intent(this, SecondActivity.class);
    final PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{backIntent, intent}, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle("from SecondActivity");
    builder.setContentText("test notification press back go main activity");
    builder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(2, builder.build());
}
 
Example 4
Source File: TracerActivity.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
private void notify(String methodName) {
    String name = this.getClass().getName();
    String[] strings = name.split("\\.");
    Notification.Builder notificationBuilder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel();
        notificationBuilder = new Notification.Builder(this, TRACER);
    } else {
        //noinspection deprecation
        notificationBuilder = new Notification.Builder(this);
    }

    Notification notification = notificationBuilder
            .setContentTitle(methodName + " " + strings[strings.length - 1])
            .setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText(name).build();
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify((int) System.currentTimeMillis(), notification);
}
 
Example 5
Source File: DownloadImageService.java    From Mizuu with Apache License 2.0 6 votes vote down vote up
private void showNotification() {
	// Setup up notification
	mBuilder = new NotificationCompat.Builder(getApplicationContext());
       mBuilder.setColor(getResources().getColor(R.color.color_primary));
	mBuilder.setSmallIcon(android.R.drawable.stat_sys_download);
	mBuilder.setTicker(getString(R.string.addingCover));
	mBuilder.setContentTitle(getString(R.string.addingCover));
	mBuilder.setContentText(getString(R.string.few_moments));
	mBuilder.setOngoing(true);
	mBuilder.setOnlyAlertOnce(true);

	// Build notification
	Notification updateNotification = mBuilder.build();

	// Show the notification
	mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	mNotificationManager.notify(NOTIFICATION_ID, updateNotification);

	// Tell the system that this is an ongoing notification, so it shouldn't be killed
	startForeground(NOTIFICATION_ID, updateNotification);
}
 
Example 6
Source File: UploadPhotoService.java    From Klyph with MIT License 6 votes vote down vote up
private void showEndNotification()
{
	if (service.get() == null)
		return;

	Service s = service.get();

	builder.setTicker(service.get().getString(R.string.upload_complete));
	builder.setContentTitle(service.get().getString(R.string.upload_complete));
	builder.setContentText(s.getString(R.string.n_uploaded_images, total - errors));
	builder.setProgress(0, 0, false);

	final NotificationManager mNotificationManager = (NotificationManager) s
			.getSystemService(Context.NOTIFICATION_SERVICE);

	mNotificationManager.notify(AttrUtil.getString(s, R.string.app_name), notificationId, builder.build());
}
 
Example 7
Source File: NotificationUtil.java    From AppUpdate with Apache License 2.0 6 votes vote down vote up
/**
 * 显示下载完成的通知,点击进行安装
 *
 * @param context     上下文
 * @param icon        图标
 * @param title       标题
 * @param content     内容
 * @param authorities Android N 授权
 * @param apk         安装包
 */
public static void showDoneNotification(Context context, int icon, String title, String content,
                                        String authorities, File apk) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //不知道为什么需要先取消之前的进度通知,才能显示完成的通知。
    manager.cancel(requireManagerNotNull().getNotifyId());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    Uri uri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        uri = FileProvider.getUriForFile(context, authorities, apk);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        uri = Uri.fromFile(apk);
    }
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder builder = builderNotification(context, icon, title, content)
            .setContentIntent(pi);
    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    manager.notify(requireManagerNotNull().getNotifyId(), notification);
}
 
Example 8
Source File: Refresher.java    From AnotherRSS with The Unlicense 6 votes vote down vote up
public void error(String title, String msg) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(_ctx);
    Bitmap largeIcon = BitmapFactory.decodeResource(_ctx.getResources(), R.mipmap.errorhint);
    mBuilder.setContentTitle(title)
            .setContentText(msg)
            .setTicker(msg)
            .setSmallIcon(R.drawable.logo_sw)
            .setLargeIcon(largeIcon)
            .setVibrate(new long[]{2000})
            .setPriority(Notification.PRIORITY_LOW);
    Notification noti = mBuilder.build();
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager mNotifyMgr =
            (NotificationManager) _ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyMgr.notify((int)System.currentTimeMillis(), noti);
}
 
Example 9
Source File: Notifications.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private void calibrationNotificationCreate(String title, String content, Intent intent, int notificationId) {
    NotificationCompat.Builder mBuilder = notificationBuilder(title, content, intent);
    mBuilder.setVibrate(vibratePattern);
    mBuilder.setLights(0xff00ff00, 300, 1000);
    if(calibration_override_silent) {
        mBuilder.setSound(Uri.parse(calibration_notification_sound), AudioAttributes.USAGE_ALARM);
    } else {
        mBuilder.setSound(Uri.parse(calibration_notification_sound));
    }

    NotificationManager mNotifyMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    //mNotifyMgr.cancel(notificationId);
    mNotifyMgr.notify(notificationId, mBuilder.build());
}
 
Example 10
Source File: AdditionService.java    From AIDLDemo with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void placeCall(final String number) throws RemoteException {

	Intent intent = new Intent(Intent.ACTION_CALL);
	intent.setData(Uri.parse("tel:" + number));
	intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	if (ActivityCompat
			.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE)
			!= PackageManager.PERMISSION_GRANTED) {

		Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
		// use System.currentTimeMillis() to have a unique ID for the pending intent
		PendingIntent pIntent = PendingIntent
				.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent1, 0);

		// build notification
		// the addAction re-use the same intent to keep the example short
		Notification n = new Notification.Builder(getApplicationContext())
				.setContentTitle("AIDL Server App")
				.setContentText("Please grant call permission from settings")
				.setSmallIcon(R.drawable.ic_launcher)
				.setContentIntent(pIntent)
				.setAutoCancel(true).build();

		NotificationManager notificationManager =
				(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		notificationManager.notify(0, n);
		return;
	}
	startActivity(intent);
}
 
Example 11
Source File: NearbyBackgroundService.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void createNotification() {
    //mNearbyDevicesMessageList = loadNearbyMessageListForNotification();
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if(mNearbyDevicesMessageList.size() == 0){
        notificationManager.cancelAll();
        return;

    }
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.putExtra(Utils.NEARBY_DEVICE_DATA, mNearbyDevicesMessageList);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivities(getApplicationContext(), OPEN_ACTIVITY_REQ, new Intent[]{intent}, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.ic_eddystone)
                    .setColor(ContextCompat.getColor(getApplicationContext(), R.color.actionBarColor))
                    .setContentTitle(getString(R.string.app_name))
                    .setContentIntent(pendingIntent);

    if(mNearbyDevicesMessageList.size() == 1) {
        mBuilder.setContentText(new String(mNearbyDevicesMessageList.get(0).getContent(), Charset.forName("UTF-8")));
    } else {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle(getString(R.string.app_name));
        inboxStyle.setSummaryText(mNearbyDevicesMessageList.size() + " beacons found");
        mBuilder.setContentText(mNearbyDevicesMessageList.size() + " beacons found");
        for (int i = 0; i < mNearbyDevicesMessageList.size(); i++) {
            inboxStyle.addLine(new String(mNearbyDevicesMessageList.get(i).getContent(), Charset.forName("UTF-8")));
        }
        mBuilder.setStyle(inboxStyle);
    }

    notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
 
Example 12
Source File: BookBoxApplication.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
/**
 * @param title
 * @param msg
 * @param cid   章节id
 */
public void showNovelUpdate(String title, String msg, String cid) {

    if (!getPreferenceUtils().getValue(Constants.NOTIFICATION_AFTERNOON, true)) {
        return;
    }

    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    //为了版本兼容  选择V7包下的NotificationCompat进行构造
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    //Ticker是状态栏显示的提示
    builder.setTicker(title);
    //第一行内容  通常作为通知栏标题
    builder.setContentTitle(title);
    //第二行内容 通常是通知正文
    builder.setContentText(msg);
    //可以点击通知栏的删除按钮删除
    builder.setAutoCancel(true);
    //系统状态栏显示的小图标
    builder.setSmallIcon(R.drawable.ic_launcher);
    int uniqueId = 0;
    if (StringUtils.isEmpty(cid)) {
        uniqueId = Constants.NOTIFI_ID_NOVEL_UPDATE;
    } else {
        uniqueId = cid.hashCode();
    }
    //下拉显示的大图标
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    Intent intent = new Intent(this, SplashActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("target", Constants.NOTIFI_ACTION_NOVEL_UPDATE);
    intent.putExtra("cid", cid);
    PendingIntent pIntent = PendingIntent.getActivity(this, uniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //点击跳转的intent
    builder.setContentIntent(pIntent);
    //通知默认的声音 震动 呼吸灯
    builder.setDefaults(NotificationCompat.DEFAULT_ALL);
    android.app.Notification notification = builder.build();
    manager.notify(uniqueId, notification);
}
 
Example 13
Source File: DaedalusVpnService.java    From Daedalus with GNU General Public License v3.0 5 votes vote down vote up
private void updateUserInterface() {
    long time = System.currentTimeMillis();
    if (time - lastUpdate >= 1000) {
        lastUpdate = time;
        if (notification != null) {
            notification.setContentTitle(getResources().getString(R.string.notice_queries) + " " + provider.getDnsQueryTimes());
            NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(NOTIFICATION_ACTIVATED, notification.build());
        }
    }
}
 
Example 14
Source File: DownloadService.java    From android-retrofit-file-download with MIT License 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_download)
            .setContentTitle("Download")
            .setContentText("Downloading File")
            .setAutoCancel(true);
    notificationManager.notify(0, notificationBuilder.build());

    initDownload();

}
 
Example 15
Source File: BleManager.java    From thunderboard-android with Apache License 2.0 5 votes vote down vote up
private void sendNotification(Beacon beacon) {

        // check if notifications are enabled and allowed for the beacon
        if (!BleUtils.checkAllowNotifications(beacon.getBluetoothAddress(), preferenceManager.getPreferences())) {
            Timber.d("Notifications not allowed for : %s, address: %s", beacon.getBluetoothName(), beacon.getBluetoothAddress());
            return;
        }

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context)
                        .setContentTitle(context.getResources().getString(R.string.app_name))
                        .setContentText(String.format("%s is nearby.", beacon.getBluetoothName()))
                        .setSmallIcon(R.drawable.ic_stat_sl_beacon)
                        .setAutoCancel(true);


        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        Intent intent = new Intent(context, DemosSelectionActivity.class);
        intent.putExtra(ThunderBoardConstants.EXTRA_DEVICE_BEACON, (Parcelable) beacon);
        stackBuilder.addParentStack(DemosSelectionActivity.class);
        stackBuilder.addNextIntent(intent);

        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(beacon.getId3().toInt(), builder.build());
    }
 
Example 16
Source File: SendNotificationLoader.java    From Easer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void _load(@ValidData @NonNull SendNotificationOperationData data, @NonNull OnResultCallback callback) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle(data.title);
    builder.setContentText(data.content);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
    NOTIFICATION_ID++;

    callback.onResult(true);
}
 
Example 17
Source File: NotificationUtil.java    From AndroidPlayground with MIT License 5 votes vote down vote up
public static void fireNotificationWithPendingIntent(PendingIntent pendingIntent,
        Context context) {
    NotificationManager notifyMgr =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifyMgr.notify(1001, getCommonBuilder(context).setContentTitle(
            context.getString(R.string.app_name))
            .setContentText("fireNotificationWithPendingIntent " + sCount)
            .setContentIntent(pendingIntent)
            .build());
    sCount++;
}
 
Example 18
Source File: Manager.java    From cordova-plugin-firebase-extended-notification with MIT License 4 votes vote down vote up
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();
}
 
Example 19
Source File: ServiceTaskWithNotificationBase.java    From edslite with GNU General Public License v2.0 4 votes vote down vote up
private void updateNotification()
   {
       NotificationManager nm = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
	if (nm != null)
		nm.notify(_taskId, _notificationBuilder.build());
}
 
Example 20
Source File: RemoteViewsAction.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
@Override void update() {
  NotificationManager manager = getService(picasso.context, NOTIFICATION_SERVICE);
  manager.notify(notificationId, notification);
}