2

I am putted my logic in android background service,which will be start on onClick action of my sticky notification.Everything working fine but problems are:-

  1. When I am lock my phone and try to click/tap on notification it requires double click/tap always.
  2. My logic is in background service but after clicked on notification Background service does not start until my mobile is unlocked.(Background service is sticky)

Below code is used for generate a sticky notification.

private void Notify() {
    Context objContext = this.cordova.getActivity();
    Intent objIntent = new Intent(objContext, ApiCallServeice.class);
    PendingIntent pi = PendingIntent.getService(objContext, intNotificationId, objIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder builder = new Notification.Builder(objContext);
    builder.setContentTitle("Click to get help.");
    builder.setAutoCancel(false);
    builder.setSmallIcon(objContext.getApplicationInfo().icon);
    builder.setOngoing(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }
    builder.setContentIntent(pi);
    builder.build();

    myNotication = builder.getNotification();
    manager.notify(intNotificationId, myNotication);
}

Please suggest me the solution or need to set any flag in my code.

1 Answer 1

1

For getting click on notification UI everwhere.We need to use the Remote View in which you can put the button overlay on whole layout and write click listener on that button

Below is the update code I am used:-

   private void Notify() {
    Context objContext=this.cordova.getActivity();
    Intent objIntent = new Intent(objContext, ApiCallServeice.class);
    PendingIntent pi = PendingIntent.getService(objContext, intNotificationId, objIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    RemoteViews objRemoteViews = new RemoteViews(objContext.getApplicationContext().getPackageName(), R.layout.your_notification_layout);
    objRemoteViews.setOnClickPendingIntent(R.id.your_notification_clickable_button, pi);

    Notification.Builder builder = new Notification.Builder(objContext);
    builder.setAutoCancel(false);
    builder.setSmallIcon(objContext.getApplicationInfo().icon);
    objRemoteViews.setImageViewResource(R.id.img_icon, objContext.getApplicationInfo().icon);
    builder.setOngoing(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }
    builder.setContent(objRemoteViews);
    builder.build();

    myNotication = builder.getNotification();
    manager.notify(intNotificationId, myNotication);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.