-3

//I had given intent in oncreate

 Intent alarmIntent = new Intent(getContext(), ServiceReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getContext(),
                0, alarmIntent, 0);

// here i am setting alarm

 public void setALARM(String time, String strDate){

            AlarmManager manager = (AlarmManager) getContext()
                    .getSystemService(Context.ALARM_SERVICE);

            String strHour=formateDateFromstring("HH:mm","HH",time);
            String strMin=formateDateFromstring("HH:mm","mm",time);
            String strdate=formateDateFromstring("yyyy-MM-dd","dd",strDate);
            String strMonth=formateDateFromstring("yyyy-MM-dd","MM",strDate);
            String strYear=formateDateFromstring("yyyy-MM-dd","yyyy",strDate);

            int hour=Integer.parseInt(strHour);
            int min=Integer.parseInt(strMin);
            int date=Integer.parseInt(strdate);
            int month=Integer.parseInt(strMonth)-1;
            int year=Integer.parseInt(strYear);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH,date);  //1-31
            cal.set(Calendar.MONTH,month);  //first month is 0!!! January is zero!!!
            cal.set(Calendar.YEAR,year);//year...

            cal.set(Calendar.HOUR_OF_DAY,hour);  //HOUR
            cal.set(Calendar.MINUTE,min);//MIN
            cal.set(Calendar.SECOND,0);


            manager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    pendingIntent);
        }

//Here is my receiver class

public class ServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.logob_icon_prestigesalon)
                .setContentTitle("ABC")
                .setContentText("time to go")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        Toast.makeText(context,"service",Toast.LENGTH_LONG).show();

        NotificationManager notificationmanager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationmanager.notify(0, builder.build());
    }
}

//I mentioned receiver class in manifest

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

  <receiver android:name=".ServiceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <!-- Will not be called unless the application explicitly enables it -->
        <receiver android:name=".DeviceBootReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

I want to set an alarm for the exact time.I checked the code in debug mode whether the debug is coming inside the onReceive if the given time comes.The debug is coming whe the given time is reached.The issue is notification is not visible.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Deepa Dinesh
  • 171
  • 2
  • 2
  • 11

2 Answers2

0

Not sure if it is your specific problem, but NotificationCompat.Builder(Context context) is deprecated in API level 26.1.0. You should use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id for 26+ devices as you can check in Notification and Notification.Builder documentation

haroldolivieri
  • 2,173
  • 18
  • 29
0

Try something like this,Have not tested.

   String channelID  = "Notification_Channel_ID";
   String channelDesr = "Notification_channel_description";
   buildNotificationChannel(channelID,channelDesr);



    public void buildNotificationChannel(String channelID, String description) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) 
            context.getSystemService(Context.NOTIFICATION_SERVICE);
           if (manager.getNotificationChannel(channelID) == null) {
                NotificationChannel channel = new NotificationChannel(channelID, description,
            NotificationManager.IMPORTANCE_LOW);
            channel.setDescription(description);
            manager.createNotificationChannel(channel);
        }
    }
}

   NotificationCompat.Builder builder = new 
             NotificationCompat.Builder(context,channelID)
            .setSmallIcon(R.drawable.logob_icon_prestigesalon)
            .setContentTitle("ABC")
            .setContentText("time to go")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
           Toast.makeText(context,"service",Toast.LENGTH_LONG).show();
           NotificationManager notificationmanager = (NotificationManager) context
              .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationmanager.notify(0, builder.build());
Ramesh Yankati
  • 1,197
  • 9
  • 13