1

I have prepared a simple test app which posts a notification on a button click:

screenshot

The source code from MainActivity.java creating the notification is displayed below:

    Button showButton = (Button) findViewById(R.id.show);
    showButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent appIntent = new Intent(mContext, MainActivity.class);
            appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            appIntent.putExtra("my_data", 12345);

            String question = getString(R.string.the_question);
            PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Notification notification = new NotificationCompat.Builder(mContext)
                    .setContentTitle(question)
                    .setContentText(question)
                    .setTicker(question)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(contentIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .build();

            mManager.notify(NOTIFY_ID, notification);
        }
    });

My question is: how to modify the notification, so that the user is asked a Yes/No question (in this case: "Do you want to open the car?") and - after she selects Yes or No to launch the same app and run a corresponding method in it (in this case: openCar() or closeCar() method).

I probably should use NotificationCompat.Action.Builder - but how exactly?

Also I am not really sure if this code is the correct code for launching an app from notification and what flags should I use:

Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

And finally I wonder if hardcodidng some random number in NOTIFY_ID is the correct way when posting notifications?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • I think you should use notification to open alert dialogue that should contain your question... and then go ahead according to answer chosen by user.. – Ankit Kumar Dec 04 '14 at 13:58
  • 1
    see http://stackoverflow.com/questions/16168553/create-custom-notification-android – Shayan Pourvatan Dec 04 '14 at 13:59
  • 1
    Use `addAction()` method with a `PendingIntent` (different) for each one. – shkschneider Dec 04 '14 at 14:00
  • 1
    Use Big style Notifications. it give you chance to implement your Functionality.Go through this link http://developer.android.com/training/notify-user/expanded.html Custom Notification http://codeversed.com/expandable-notifications-android/ – Tarun Sharma Dec 04 '14 at 14:01
  • Yes +1 shkschneider, I understand that I should use `addaction` with some kind of intents, but what is the exact source code for that? – Alexander Farber Dec 04 '14 at 14:01
  • Yes, I've read http://developer.android.com/training/notify-user/expanded.html and realized that I need exactly that (Yes/No question in big style notification, but also as backup for older devices - in the activity) - but can not find the source code for that sample. Does anybody please know where to find the source code? – Alexander Farber Dec 07 '14 at 10:17
  • Okay, found it here https://github.com/android/platform_development search for "pingme" – Alexander Farber Dec 07 '14 at 10:35

2 Answers2

1

Here is a source code I used for notification with Login/Register action.

private void sendNotification(String message, String title) {
  try {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Intent secondActivityIntent = new Intent(this, SecondActivity.class);
    PendingIntent secondActivityPendingIntent = PendingIntent.getActivity(this, 0 , secondActivityIntent, PendingIntent.FLAG_ONE_SHOT);

    Intent thirdActivityIntent = new Intent(this, ThridActivity.class);
    PendingIntent thirdActivityPendingIntent = PendingIntent.getActivity(this, 0 , thirdActivityIntent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_3d_rotation_white_36dp)
            .setContentTitle(title)
            .setContentText(message)
            .addAction(R.drawable.ic_lock_open_cyan_600_24dp,"Login",secondActivityPendingIntent)
            .addAction(R.drawable.ic_lock_pink_700_24dp,"Register",thirdActivityPendingIntent)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  } catch (Exception e) {
    e.printStackTrace();
  }
}

To use it: simply call this method sendNotification(String yourMessage, String yourTitle)

e.g. sendNotification("Hello Message", "Hello Title")

Here is a snapshot of the output Android Notification with Actions

Akinola Olayinka
  • 841
  • 7
  • 11
0

Notify user on pending Intent.. an example is here..

 public void notifyUser() {

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

    Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // use the flag FLAG_UPDATE_CURRENT to override any notification already
    // there
    pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "Question.....?????", System.currentTimeMillis());  
    notification.flags = Notification.FLAG_AUTO_CANCEL
            | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;

    notification.setLatestEventInfo(this, "title",
            "Explanation of question..", pendingIntent);
    // 10 is a random number I chose to act as the id for this notification
    notificationManager.notify(10, notification);
}
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
  • 1
    Does not your source code merely show how to post a notification? (Which is already done in my test app). My problem is to react to the users Yes/No selection. – Alexander Farber Dec 04 '14 at 15:02