Scheduled notifications don't appear

84 views Asked by At

I'm developing my first Android app, Todo List, and i have to schedule notification at custom date(in 24 hr format) & time with date & timepicker.

Notifications don't appear as scheduled !

Can someone tell me what is the problem with my code?

My code : Manifest :

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.todo.amine.mytodo"
    android:versionCode="2"
    android:versionName="1.1">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
        <service android:name=".MyAlarm"
            android:enabled="true" />

        <receiver android:name=".MyReceiver">
            </receiver>
        </application>

        </manifest>

CreateNotify Method :

     public void createNotify(int month, int year, int day, int hour, int m){

        Calendar c = Calendar.getInstance();
        Date temp = c.getTime();
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
       /** calendar.set(Calendar.AM_PM,Calendar.PM);**/
        Date pm = calendar.getTime();

        if(pm.after(temp))
        {

            Intent myIntent = new Intent(Main.this, MyReceiver.class);
         pendingIntent = PendingIntent.getBroadcast(Main.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);


    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
        }

    }

MyAlarm.class

     import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;


    public class MyAlarm extends Service
    {

        private NotificationManager mManager;

        @Override
        public IBinder onBind(Intent arg0)
        {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onCreate()
        {
            // TODO Auto-generated method stub
            super.onCreate();
        }

        @SuppressWarnings("static-access")
        @Override
        public void onStart(Intent intent, int startId)
        {
            super.onStart(intent, startId);



   mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),Main.class);

    Notification notification = new Notification(R.drawable.ic_appxhdpi,"This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), `"AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);`

    mManager.notify(0, notification);
        }

        @Override
        public void onDestroy()
        {
            // TODO Auto-generated method stub
            super.onDestroy();
        }

    }

MyReceive.class

        import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    public class MyReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            Intent service1 = new Intent(context, MyAlarm.class);
            context.startService(service1);

        }
    }
0

There are 0 answers