Pregunta

I'm making an app that has a repeating alarm, that checks the date from an api, if the "date" is true, it will send a notification. The commented out notification is what I'm working with, as it works in My MainActivity class, I just can't get it notifying in my Alarm class. Any ideas?

    public class Alarm extends BroadcastReceiver 
    {    

         @Override
         public void onReceive(Context context, Intent intent) 
         {   
             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
             wl.acquire();

             HttpClient client = new DefaultHttpClient();
             HttpGet request = new HttpGet();
             try {
                request.setURI(new URI("exampleTimeUrl"));
                HttpResponse response = client.execute(request);

                    HttpEntity entity = response.getEntity();               
                    String responseText = EntityUtils.toString(entity);
                    JSONObject jsonObj = new JSONObject(responseText);
                    String istheday = jsonObj.getString("birthday");


                    if (istheday=="true"){


                        /// notify
                    /*  
                    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification notify = new Notification(android.R.drawable.stat_notify_more, "Happy Birthday", System.currentTimeMillis());
                    Context contextA = MainActivity.this;
                    CharSequence title = "happy birthday";
                    CharSequence details = "yay";
                    Intent intent2 = new Intent(contextA, MainActivity.class);
                    PendingIntent pending = PendingIntent.getActivity(contextA, 0, intent2, 0);
                    notify.setLatestEventInfo(contextA, title, details, pending);
                    nm.notify(0, notify);
                  */

                        /// end of notify





                    }


            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


             // end your code here



             wl.release();

         }

     public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 8000, pi); // Millisec * Second * Minute - will obviously increase this to run every day
     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }
 }
¿Fue útil?

Solución

Check this :

Notification notification;
NotificationManager nm;
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder;
private static final int NOTIFICATION_ID = 1;
   @Override
    public void onReceive(Context context, Intent arg1) {
        showNotification(context);
    }

    private void showNotification(Context context) {
            Uri alarmSound = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (alarmSound == null) {
                alarmSound = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }

            mBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.image)
                    .setSound(alarmSound)
                    .setContentTitle("New Locations Found")
                    .setContentText(
                            value
                                    + " New Locations has been assigned to you");

            mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            mBuilder.setAutoCancel(true);

            Intent resultIntent = new Intent(this, HeliosActivity.class);
            resultIntent.putExtra("skiphomescreen", true);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(HeliosActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    }  
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top