0

I am creating Daily Notification App, where i am allowing user to set time. To develop this feature i am using AlarmReceiver and BroadcastReceiver.

I want to show notification message in Dialog box/Alert Box when user click on Notification.

Can someone help me to achieve this functionality?

Below is the code where i am setting Notification message.

public class AlarmReceiver extends BroadcastReceiver {

String TAG = "AlarmReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if (intent.getAction() != null && context != null) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            // Set the alarm here.
            Log.d(TAG, "onReceive: BOOT_COMPLETED");
            LocalData localData = new LocalData(context);
            NotificationScheduler.setReminder(context, AlarmReceiver.class, localData.get_hour(), localData.get_min());
            return;
        }
    }

    Log.d(TAG, "onReceive: ");

    //Trigger the notification
    NotificationScheduler.showNotification(context, MainActivity.class,
            "You have New Notification", "check it now?");

    }
}

Output should like this after clicking on Notification...

enter image description here

VikaS GuttE
  • 1,636
  • 2
  • 14
  • 38

3 Answers3

0

Code of notification with pending intent that open up an activity and on that activity should code for that popup

Notification notif = new Notification(R.drawable.ic_launcher,"List of Contacts...", 
System.currentTimeMillis());
Intent notificationIntent = new Intent(context,AllContacts.class);
notificationIntent.putExtra("from", "notification") 
notificationIntent.putExtra("message", "yourmessage") 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,            
notificationIntent, 0);
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);

and in AllContacts.class in onCreate

if(getIntent.getStringExtras("from").equals("notification")){
   //popup show with message
}
Sohel S9
  • 246
  • 1
  • 15
0
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID, 
            notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

just make the PendingIntent open up one of your Activities and have your Activity be complete transparent and just open a Dialog.

EDIT: IF you want to open an Activity from notification click event:

Assuming that notif is your Notification object:

Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;

For more detail

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
  • thank you for your comment.. Could you please explain little bit... where i need put your code...in main activity or in AlarmReceiver class... – VikaS GuttE Feb 21 '19 at 10:39
  • in alarm receiver where you are getting notification – Tejas Pandya Feb 21 '19 at 10:40
  • Ok let me check...one question if here `YourActivityClassHere.class` is `MainActivity.class` then where i need to write code for Alert/Popup/DialogBox and how i can call that onClick of Notification – VikaS GuttE Feb 21 '19 at 10:43
  • @VikaS no you need to make your activity transparent . is your mainactivity transparent? – Tejas Pandya Feb 21 '19 at 10:52
  • MainActivity is not transparent... i want to show that on MainActivity only... by crating Alert Box....... I tried to add your code in `AlarmReceiver` but it showing error for `setFlag` & `UNIQUE_ID` – VikaS GuttE Feb 21 '19 at 11:00
  • @VikaS make another activity which is transparent not mainactivity. you dont need to add unique id , remove it – Tejas Pandya Feb 21 '19 at 11:06
0

Change your AlarmReceiver class as below

public class AlarmReceiver extends BroadcastReceiver {

String TAG = "AlarmReceiver";
public static String NotificationMsg="You have 5 unwatched videos", NotificationTitle = "Watch them now?";
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if (intent.getAction() != null && context != null) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            // Set the alarm here.
            Log.d(TAG, "onReceive: BOOT_COMPLETED");
            LocalData localData = new LocalData(context);
            NotificationScheduler.setReminder(context, AlarmReceiver.class, localData.get_hour(), localData.get_min());
            return;
        }
    }

    Log.d(TAG, "onReceive: ");

    //Trigger the notification
    NotificationScheduler.showNotification(context, NotificationActivity.class,
            NotificationTitle, NotificationMsg);

}
}

Then Create New Activity here I created NotificationActivity

public class NotificationActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ShowDialog();
}

@SuppressLint("ResourceAsColor")
public void ShowDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(NotificationActivity.this);
    builder.setMessage("\n" + NotificationMsg);
    TextView title = new TextView(NotificationActivity.this);
    title.setText(NotificationTitle);
    title.setBackgroundColor(Color.DKGRAY);
    title.setPadding(20, 20, 20, 20);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(Color.WHITE);
    title.setTextSize(20);
    builder.setCustomTitle(title)
            .setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            //finishAffinity();
                        }
                    });
    builder.show();
}
}

Output is here -

Output will be like this

Hope this will work... Please don't forgot to accept answer and up vote...