0

I have followed these tutorial and I got that notification. But I want to set the count and make it visible with the icon and I want to drag and close the notification like facebook.

This is my code. To increase the notification I have followed this link

 @Override 
  public void onCreate() {
    super.onCreate();
    context = this;
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    Bitmap bm = BitmapFactory.decodeResource(getResources(),
              R.drawable.ic_launcher);
    chatHead = new ImageView(this);
    chatHead.setImageBitmap(getCircleBitmap(bm, 200));
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;
    setBadge(context, count++);
    chatHead.setOnTouchListener(new View.OnTouchListener() {
          private int initialX;
          private int initialY;
          private float initialTouchX;
          private float initialTouchY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // TODO Auto-generated method stub
            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();

                return true;
              case MotionEvent.ACTION_UP:
                return true;
              case MotionEvent.ACTION_MOVE:
                params.x = initialX + (int) (event.getRawX() - initialTouchX);
                params.y = initialY + (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(chatHead, params);

                return true;
            }
            return false;
        }
    });
    windowManager.addView(chatHead, params);

  }
  public static void setBadge(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }
        Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", count);
        intent.putExtra("badge_count_package_name", context.getPackageName());
        intent.putExtra("badge_count_class_name", launcherClassName);
        context.sendBroadcast(intent);
    }

    public static String getLauncherClassName(Context context) {

        PackageManager pm = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                String className = resolveInfo.activityInfo.name;
                return className;
            }
        }
        return null;
    }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      return START_STICKY;
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
  }
  private Bitmap getCircleBitmap(Bitmap bitmap, int pixels) {
      final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
       bitmap.getHeight(), Bitmap.Config.ARGB_8888);
      final Canvas canvas = new Canvas(output);
     // int pixels;
      final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;
        paint.setShadowLayer(2, 2, 2, 0xff424242);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
      canvas.drawBitmap(bitmap, rect, rect, paint);

      bitmap.recycle();

      return output;
     }

I also want to make it visible the count with GCM notification. I set the count with the GCM message. But it is not visible.

private void sendNotification(String msg) {
    int numMessages = 0;
    Intent inservice = new Intent(this, ChatService.class);
    startService(inservice);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_stat_gcm)
            .setContentTitle("Contact Received")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setNumber(numMessages++)
            .setSound(Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.sounds1));
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

Could anyone give me some working example?

Community
  • 1
  • 1

1 Answers1

0

I do not think there is a way to do this if you mean for it to be in the notifications window. If it's in the notification window, you should consider using the number attribute for notifications.

If you insist on using a badge like element, you can check out Android ViewBadger, but I don't think you can use this in your notifications window.

Andy
  • 2,308
  • 3
  • 14
  • 20