Search code examples
androidandroid-intentbroadcastreceiver

Android Broadcast Reciever to open an intent


I have an android app which needs to generate a notification in regular intervals and when we click on the notification the message shown in the notification will be shown inside a TextView inside an activity of the Original application.

For that I've created an AlarmReceiver.java Class.

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("ME", "Notification started");

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                                                    .setSmallIcon(R.drawable.ic_noticon)
                                                    .setContentTitle("Notification")
                                                    .setContentText("You have to open app to see this message in detail");


            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());


            Intent resultIntent = new Intent("com.app.AlarmBack");
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(AlarmBack.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_ONE_SHOT);
            mBuilder.setContentIntent(resultPendingIntent);


    }    

also I've created the class for an intent called AlarmBack.java

public class AlarmBack extends MainActivity {


TextView tv_alert_back_display;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    getLayoutInflater().inflate(R.layout.alert_back, frameLayout);
    mDrawerList.setItemChecked(position, true);
    setTitle(mTitle);


    tv_alert_back_display = (TextView) findViewById(R.id.tv_alert_back_display);


    tv_alert_back_display.setText("This is the message that needs your attension");
}}

but the problem is that this doesn't work. It's not opening the intent that that want it to open. So, can somebody please help to solve my problem.


Solution

  • Call this in your AlarmReceiver OnRecieve method

    int taskID = 1;
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(taskID, getNotification(context, "title", "content"));
    

    And add this method in your Alarm Receiver

    private Notification getNotification(Context context, String title, String content) {
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle(title);
        builder.setContentText(content);
        builder.setSmallIcon(R.drawable.icon_64);
        builder.setAutoCancel(true);
        builder.setDefaults(android.app.Notification.DEFAULT_ALL);
    
    
        Intent resultIntent = new Intent(context, SplashActivity.class); //Activity that you want to open
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(SplashActivity.class);              // Activity that you want to open
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        builder.setContentIntent(resultPendingIntent);
        return builder.getNotification();
    }