0

I am developing a flashLight app and I faced a problem in notification which is : When I press the notification text I want to turn of flash and close the whole app , How can I modified the notification method to do that ?? I tried to make anthor activity and put turn of and close app methods in it, but it does not work . please help and thanks in advance .

this is my notification method

public void getNotification()
{
    Intent intent = new Intent();
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentIntent(pIntent)
            .setContentTitle(res.getString(R.string.notification_title))
            .setContentText(res.getString(R.string.notification_text))
            .setSmallIcon(R.drawable.icon)
            .setAutoCancel(true)
            .setTicker(getString(R.string.notification_ticker_msg));
    // Build the notification:
    Notification notification = builder.build();

    // Get the notification manager:
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Publish the notification:
    final int notificationId = 0;
    notificationManager.notify(notificationId, notification);
}
Ali Bahaj
  • 11
  • 1
  • 5

3 Answers3

1

If you use the same Activity, then the onCreate method will be called again. You can send one extra with your Intent that indicates it is an Intent generated from the click of notification. In your Activity onCreate, check for this extra and call finish() if it is present.

public void getNotification() {
    Intent intent = new Intent(this, FlashActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("origin", "notification");

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentIntent(pIntent)
            .setContentTitle(res.getString(R.string.notification_title))
            .setContentText(res.getString(R.string.notification_text))
            .setSmallIcon(R.drawable.icon)
            .setAutoCancel(true)
            .setTicker(getString(R.string.notification_ticker_msg));
    // Build the notification:
    Notification notification = builder.build();

    // Get the notification manager:
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Publish the notification:
    final int notificationId = 0;
    notificationManager.notify(notificationId, notification);
}

And in your onCreate method in FlashActivity check the extra.

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    if("notification".equals(getIntent().getStringExtra("origin"))) {
        finish();
    }
}
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • You copied from OP "Intent intent = new Intent();" - maybe better something like "Intent intent = new Intent(this, MyActivity.class);" – Bö macht Blau May 03 '16 at 07:10
  • Right, and even I believe, it will be better for them to use FLAG_ACTIVITY_SINGLE_TOP or FLAG_ACTIVITY_SINGLE_TASK. – jaibatrik May 03 '16 at 07:11
  • 1
    about the flags - they seem to be ignored when used with a notification (although they work when used with home screen widget) I just tried to use "FLAG_ACTIVITY_REORDER_TO_FRONT" from a notification and *it re-created the activity* anyway. The same goes for "FLAG_ACTIVITY_SINGLE_TOP". Would be better to avoid that if you're just going to finish the activity but I don't know how. – Bö macht Blau May 03 '16 at 07:19
  • Makes sense. And looks like FLAG_ACTIVITY_SINGLE_TASK is deprected. – jaibatrik May 03 '16 at 07:22
  • 1
    So I think the best you can do performance-wise is to check the Intent before "setContentView()". – Bö macht Blau May 03 '16 at 07:24
  • Thank you for your answer @jaibatrik , but it does not close the activity just restore the activity and the flash is on – Ali Bahaj May 03 '16 at 08:19
0

I believe you can use finish() on your Activity when the notification ils pressed.

EDIT: How to close any activity of my application by clicking on a notification?

Community
  • 1
  • 1
Omar Aflak
  • 2,918
  • 21
  • 39
  • A flashlight app is typically a one-activity-app. If you have more than one activity, the link you posted is helpful (but being just a link it should have been posted as a comment, not as an answer). If you want to finish just one activity, the methods suggested there are way too expensive. – Bö macht Blau May 03 '16 at 07:34
0

You want to use PendingIntent.

 Intent resultIntent = new Intent(this, MainActivity.class);
 resultIntent.putExtra("FINISH",true);
 PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
        this,
        0,
        resultIntent,
        PendingIntent.FLAG_UPDATE_CURRENT
    );

builder.setContentIntent(resultPendingIntent);

// Build the notification:
    Notification notification = builder.build();
//rest of your methods to show notification

In your MainActivity update depending on your code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent){
     setContentView(R.layout.activity_main);
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("FINISH"))
        {
           finish();
        }
    }


}
Sanif SS
  • 602
  • 5
  • 18