2016-12-21 3 views
-1

Привет, я делаю приложение для отслеживания привычек, и когда новая привычка создается пользователем, я вызываю метод sendNotification() для вызова уведомлений в указанное пользователем время. Я хочу показывать эти уведомления каждый день в указанное пользователем время. Теперь уведомления появляются, когда приложение запущено или когда приложение сведено к минимуму, но когда я закрываю приложение (не из настроек), отображаются уведомления. Вот мой код:Локальные уведомления, которые не отображаются, когда приложение закрыто, используя последние приложения от пользователя.

private void sendNotification(){ 
    NotificationReceiver.setupAlarm(this, notificationCalendar); 
} 

public class NotificationReceiver extends WakefulBroadcastReceiver { 

public NotificationReceiver() { 
} 

public static void setupAlarm(Context context, Calendar notificationCalendar) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent alarmIntent = getStartPendingIntent(context); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationCalendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, alarmIntent); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); 
    startWakefulService(context, serviceIntent); 
} 

private static PendingIntent getStartPendingIntent(Context context) { 
    Intent intent = new Intent(context, NotificationReceiver.class); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 
} 

public class NotificationIntentService extends IntentService { 

private static final int NOTIFICATION_ID = 1; 

public NotificationIntentService() { 
    super(NotificationIntentService.class.getSimpleName()); 
} 

public static Intent createIntentStartNotificationService(Context context) { 
    return new Intent(context, NotificationIntentService.class); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    try{ 
    processStartNotification(); 
    }finally { 
     WakefulBroadcastReceiver.completeWakefulIntent(intent); 
    } 
} 

private void processStartNotification() { 
    // Do something. For example, fetch fresh data from backend to create a rich notification? 
    NotificationManager notificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Habit Time") 
      .setContentText("Hey time for your habit") 
      .setAutoCancel(true) 
      .setContentIntent(pendingIntent) 
      .setSound(alarmSound) 
      .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 

    notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build()); 
} 
} 

//Manifest file 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <receiver android:name=".notification.NotificationReceiver"/> 

    <service 
     android:name=".notification.NotificationIntentService" 
     android:enabled="true" 
     android:exported="false"/> 
+1

Должен ли я использовать сервис для уведомлений? –

+0

почему его не работает с широковещательным приемником –

+0

ваш вопрос запутан, поэтому уведомление не отображается, когда приложение убито ... ?? – rafsanahmad007

ответ

0

Уведомление не будет отображаться, пока вы не сделаете фоновую службу для них. Создайте фоновый сервис и отправьте широковещательную передачу с этой службы, эта служба будет поддерживать погоду, запущенную вашим приложением или нет. Вы можете проверить подробный ответ this.

Создайте класс NotificationService и расширьте его, как показано ниже.

public class NotificationService extends Service{ 

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


@SuppressWarnings("deprecation") 
@Override 
public void onStart(Intent intent, int startId) { 
    // TODO Auto-generated method stub 
    super.onStart(intent, startId); 

    /* send your notification from here, in timely manner */ 
} 

    } 

Start от вашей деятельности, как этот

Intent i = new Intent("com.example.package.NotificationService"); 
      startService(i); 

Этот код написан, чтобы дать вам идею. Я не проверял.

+0

I использовал этот метод, но уведомления показывают, когда приложение находится в памяти! –

+0

Где ваш класс обслуживания? –

+0

Я использовал это в своем коде точно, он был написан, но не работает, когда приложение не в фоновом режиме –

0

Попробуйте следующий код: он создает сигнал на определенное время в течение дня, используя alarmanager и повторять его ежедневно ...

Объявить

Calendar cal_alarm; 

Теперь установите объект каландра для определенного времени сигнализация

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MM yyyy hh:mm a"); 
String time=""21 12 2016 8:10 AM" 
cal_alarm = Calendar.getInstance(); 
cal_alarm.setTime(dateFormat.parse(time)); 

Установка сигнализации

public void set_alarm() { 
    Calendar calNow = Calendar.getInstance(); 
    long current_time = calNow.getTimeInMillis(); 
    Log.d("ALARM CALENDER VALUES", cal_alarm.toString()); 
    long alarm_time_in_millis = cal_alarm.getTimeInMillis(); 
    //check if time is alreday passed or not 
    if (alarm_time_in_millis > current_time) { 
     new Alarm_task(getApplicationContext(), cal_alarm).run(); 
} 

public class Alarm_task implements Runnable { 

    // The date selected for the alarm 
    private final Calendar cal; 
    // The android system alarm manager 
    private final AlarmManager am; 
    // Your context to retrieve the alarm manager from 
    private final Context context; 
    long alarm_time2; 
    int _id; 

    public Alarm_task(Context context, Calendar cal) { 
     this.context = context; 
     this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     this.cal = cal; 
     this._id = (int) System.currentTimeMillis(); 
     alarm_time2 = cal.getTimeInMillis(); 
     //Toast.makeText(getActivity(), alarm_time2 + " ", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void run() { 
     // Request to start are service when the alarm date is upon us 
     // We don't start an activity as we just want to pop up a notification into the system bar not a full activity 
     Intent i = new Intent("com.package_name"); 
     i.setAction("com.package_name"); 
     /** Creating a Pending Intent */ 
     PendingIntent operation = PendingIntent.getActivity(getApplicationContext(), _id, i, PendingIntent.FLAG_UPDATE_CURRENT); 
     /** Converting the date and time in to milliseconds elapsed since epoch */ 
     long alarm_time = cal.getTimeInMillis(); 
     /** Setting an alarm, which invokes the operation at alart_time each day*/ 
     am.setRepeating(AlarmManager.RTC_WAKEUP, alarm_time, AlarmManager.INTERVAL_DAY, operation); 
    } 
} 

Теперь в манифесте определить явную активность, обрабатывать намерение тревоги и показать всплывающее диалоговое окно и уведомление во время тревоги

<activity 
     android:name=".Prereminder" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      //same as your alarm task 
      <action android:name="com.package_name" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

Теперь в вашем Prereminder.java

public class Prereminder extends FragmentActivity { 
    String msg = "Helloo"; 
    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_alarm); 
     sendNotification(); 
     /** Creating an Alert Dialog Window */ 
     Reminder_alert alert = new Reminder_alert(); 
     /** Opening the Alert Dialog Window */ 
     alert.show(getSupportFragmentManager(), "Reminder_alert"); 
    } 


    private void sendNotification() { 
     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     //handle notification on click 
     Intent myintent = new Intent(this, Home_page.class); 
     myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT); 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.logo_small) 
         .setContentTitle("Alarm") 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
         .setContentText(msg) 
         .setAutoCancel(true); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 
} 

Теперь ваш класс Reminder_alert показать всплывающее диалоговое окно во время тревоги:

public class Reminder_alert extends DialogFragment { 

    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */ 
     getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 


     /** Creating a alert dialog builder */ 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     /** Setting title for the alert dialog */ 
     builder.setTitle("ALARM ON"); 

     /** Setting the content for the alert dialog */ 
     builder.setMessage("WAKE UP NOW"); 

     /** Defining an OK button event listener */ 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dismiss(); 
      } 
     }); 

     /** Creating the alert dialog window */ 
     return builder.create(); 
    } 

    /** 
    * The application should be exit, if the user presses the back button 
    */ 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     getActivity().finish(); 
    } 
} 
0

1- простой способ использовать эту функцию для создания ala гт

private void createAlarm(Date start_alarm_date, String schedual_type ,String schedule_id){ 
    AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(this, PushLocalNotification.AlarmReceiver.class); 
    intent.setAction("Your_Action_Name"); //this action you will use later 
    intent.putExtra("Extra", any_extra_you_want_add);// remove if you want add extra 
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 


    // Set the alarm to start at specific date 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 

    // repeat it "daily": 
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
        AlarmManager.INTERVAL_DAY, alarmIntent); 

2- создать IntentService и сделать ваш приемник расширить BroadcastReceiver

public class PushLocalNotification extends IntentService { 

private NotificationManager mNotificationManager; 
private String mScheduleID; 
NotificationCompat.Builder builder; 

public PushLocalNotification() { 
    super("pushLocalNotification"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    // call create local notification here 
} 


public static class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("Your_action_Name")) { 
      // call service here. 
      Intent sendIntent = new Intent(context, PushLocalNotification.class); 
      sendIntent.putExtra("Extra_name", intent.getStringExtra("Previos_extra_you_add_before")); 
      context.startService(sendIntent); 
     } 
    } 
} 
private void createNotification() { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

    //Create Intent to launch this Activity again if the notification is clicked. 
    Intent i = new Intent(this, MainActivity.class); 
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(this, 0, i, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(intent); 

    // Sets the ticker text 
    builder.setTicker(getResources().getString(R.string.custom_notification)); 

    // Sets the small icon for the ticker 
    builder.setSmallIcon(R.drawable.ic_stat_custom); 

    // Cancel the notification when clicked 
    builder.setAutoCancel(true); 

    // Build the notification 
    Notification notification = builder.build(); 

    // Inflate the notification layout as RemoteViews 
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); 

    // Set text on a TextView in the RemoteViews programmatically. 
    final String time = DateFormat.getTimeInstance().format(new Date()).toString(); 
    final String text = getResources().getString(R.string.collapsed, time); 
    contentView.setTextViewText(R.id.textView, text); 

    /* Workaround: Need to set the content view here directly on the notification. 
    * NotificationCompatBuilder contains a bug that prevents this from working on platform 
    * versions HoneyComb. 
    * See https://code.google.com/p/android/issues/detail?id=30495 
    */ 
    notification.contentView = contentView; 

    // Add a big content view to the notification if supported. 
    // Support for expanded notifications was added in API level 16. 
    // (The normal contentView is shown when the notification is collapsed, when expanded the 
    // big content view set here is displayed.) 
    if (Build.VERSION.SDK_INT >= 16) { 
     // Inflate and set the layout for the expanded notification view 
     RemoteViews expandedView = 
       new RemoteViews(getPackageName(), R.layout.notification_expanded); 
     notification.bigContentView = expandedView; 
    } 

    // START_INCLUDE(notify) 
    // Use the NotificationManager to show the notification 
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    nm.notify(0, notification); 
} 

}

3- последний шаг не забудьте объявить службу и приемник внутри файла манифеста

<receiver 
     android:name=".PushLocalNotification$AlarmReceiver" 
     android:enabled="true"> 
     <intent-filter> 
      <action android:name="Your_Action_name:)" /> 
     </intent-filter> 
    </receiver> 

    <service android:name=".PushLocalNotification" /> 

быть терпеливым и получайте удовольствие :)

+0

Спасибо, но ваш код также работает только при запуске приложения или в фоновом режиме –