Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to show Dialog onClick of Notification without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
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…
Answer
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 –
Hope this will work… Please don’t forgot to accept answer and up vote…
We are here to answer your question about How to show Dialog onClick of Notification - If you find the proper solution, please don't forgot to share this with your team members.