androidwhatsapp

How to schedule a message in our application to WhatsApp?


I would like to send, schedule text messages in WhatsApp from my application. Is it possible to do that?

Currently, I can open WhatsApp using this code

Intent i=getpackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity(i);

However, is it possible to schedule a message from our application to WhatsApp?


Solution

  • You can use the AlarmManager for schedule any task for the future.. In your Activity/Fragment use this lines of code for schedule any task:-

     Intent myIntent = new Intent(AlaramClass.this, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(AlaramClass.this, 0, myIntent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, "SPECIFY_YOUR_TIME_HERE_TO_SCHEDULE_TASK", pendingIntent);
    

    And than create the receiver to receive future task

    public class AlarmReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
    
            Intent i=getpackageManager().getLaunchIntentForPackage("com.whatsapp");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
    
        }
    }
    

    And do not forget the entry for Receiver inside the Manifest (inside the <application>.....</application>)

              <receiver
                android:name=".AlarmReceiver"
                android:exported="true" >
            </receiver>
    

    And u need to add the WAKE_LOCK permission for it like below:-

    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>