0

特定の日時に特定のイベントを思い出させるアプリを開発中です。私は通知を正しく設定しましたが、時にはうまく動作しないことがあります。通知が特定の日時に表示されない

私は受信機クラス(BuddhaEventクラスからMyreceiverクラスへの送信位置)にclickを送信するために余分なものを使用しています。したがって、ユーザーが位置1をクリックすると、メッセージクラスから通知1がトリガーされます。私はそれぞれの位置の呼び出しにログステートメントを置いていますそれを確認してください。

ログ文の出力は次のようである:

POSITION0 : 0 POSITION0 : 0 POSITION1: 1 POSITION0 : 0 POSITION2 : 2

をそして、これは4つの通知がパターンあり

POSITION0 : 0 

POSITION0 : 0 POSITION1: 1 POSITION0 : 0 POSITION2 : 2 POSITION0 : 0 POSITION3 : 3

示されたログ文ですここでは、0は通知の前に常に呼び出されています!

私は何が起こっているのか分かりません。最初の通知はうまくいきますが、それ以降は通知はランダムです。ほとんどの時間通知2と3は同時に発生し、時には2が3を待っています。 私が気付いたもう一つの問題は、私が通知用にスライドすると、すべての通知が同じ配信時間を持つということです。私は各通知ごとに異なるIDを設定しました。

私が考えていることは、余分に置いて余分にすることは間違っているということです。誰かが問題を理解するのを助けることができれば、本当に感謝します。

更新:私がリストの最初の項目をクリックすると、通知1が表示されます。項目2をクリックすると、通知1と2が同時に表示されます。私が項目3をクリックすると、通知3と1が表示されます。事前に おかげ

アップデート2:PROGRAMは0 EVERY TIMEを呼んでいたが、私は別の何かにPUTのPOSITION0を変更することを決め、今ONLYつの通知は、一度にSHOWS(時間が経過した場合にのみ動作します)が、私はしようとすると、通知のスケジュールを設定するには、同じ問題が終了する(同じ時刻に2つの通知を表示するなど)。

これは私のメインクラス `パブリッククラスBuddhaEventがAppCompatActivityがView.OnClickListener { 公共リストはmyList =新しいArrayListを()を実装して延びています。 android.support.v4.app.NotificationCompat.Builder通知;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_buddhism_event); 


    fill_eventsList(); 
    fill_ListView(); 
    clickResponse(); 

    Button buddah_button_event = (Button) findViewById(R.id.button_event_back_buddhism); 
    buddah_button_event.setOnClickListener(this); 


} 

private void fill_eventsList() { 
    myList.add(new list("Jan-24", R.drawable.chris, "Mahayana countries the new year starts on the first full moon day in January.", "Mahayana New Year")); 
    myList.add(new list("Feb-2", R.drawable.chris, "Chinese festival celebrated at the turn of the traditional lunisolar Chinese calendar.", "Chinese New Year")); 
    myList.add(new list("Feb-15", R.drawable.chris, "Celebrates the day when the Buddha is said to have achieved Parinirvana, or complete Nirvana", "Nirvana Day")); 
    myList.add(new list("Mar-23", R.drawable.chris, "Celebration in honour of the Sangha, or the Buddhist community", "Magha Puja Day")); 
    myList.add(new list("Apr-22", R.drawable.chris, "Buddhist New Year", "Theravada New Year")); 
    myList.add(new list("May-15", R.drawable.chris, "Celebrates the Buddha's birthday", "Wesak - Buddha Day")); 
    myList.add(new list("Jul-11", R.drawable.chris, "Honor the spirits of one's ancestors.", "Obon")); 
    myList.add(new list("Jul-19", R.drawable.chris, "Celebrates Buddha's teachings of peace and enlightenment", "Asala - Dharma Day")); 
    myList.add(new list("Dec-8", R.drawable.chris, "The day that the historical Buddha, Siddhartha Gautama (Shakyamuni), experienced enlightenment", "Bodhi Day")); 

} 

private void fill_ListView() { 
    ArrayAdapter<list> listArrayAdapter = new myListAdapter(); 
    ListView list = (ListView) findViewById(R.id.list_eventsView); 
    list.setAdapter(listArrayAdapter); 

} 

public void clickResponse() { 
    ListView l = (ListView) findViewById(R.id.list_eventsView); 
    if (l != null) { 
     l.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, final View viewClicked, 
            final int position, long id) { 


       AlertDialog.Builder alert = new AlertDialog.Builder(
         BuddhaEvent.this); 
       alert.setTitle("Reminder!!"); 
       alert.setMessage("Do you want to be reminded of this event in future?"); 
       alert.setPositiveButton("YES", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("OUTSIDE", "CHECKING WHICH IS CLICKED : " + which); 

         if (position == 0) { 
          Calendar calendar = Calendar.getInstance(); 
          calendar.set(Calendar.MONTH, 6); 
          calendar.set(Calendar.DAY_OF_MONTH, 20); 
          calendar.set(Calendar.HOUR_OF_DAY, 21); 
          calendar.set(Calendar.MINUTE, 2); 
          calendar.set(Calendar.SECOND, 0); 

          Intent intent = new Intent(getApplicationContext(), MyReceiver.class); 
          intent.putExtra("position0", position); 


          PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

          AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
          alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

         } 

         if (position == 1) { 
          Calendar calendar2 = Calendar.getInstance(); 

          calendar2.set(Calendar.MONTH, 6); 
          calendar2.set(Calendar.DAY_OF_MONTH, 20); 

          calendar2.set(Calendar.HOUR_OF_DAY, 21); 
          calendar2.set(Calendar.MINUTE, 3); 
          calendar2.set(Calendar.SECOND, 0); 


          Intent intent2 = new Intent(getApplicationContext(), MyReceiver.class); 
          intent2.putExtra("position1",position); 



          PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent2, PendingIntent.FLAG_UPDATE_CURRENT); 

          AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE); 
          alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), pendingIntent2); 
         } 

         if (position == 2) { 

          Calendar calendar3 = Calendar.getInstance(); 

          calendar3.set(Calendar.MONTH, 6); 
          calendar3.set(Calendar.DAY_OF_MONTH, 20); 

          calendar3.set(Calendar.HOUR_OF_DAY, 21); 
          calendar3.set(Calendar.MINUTE, 4); 
          calendar3.set(Calendar.SECOND, 0); 


          Intent intent3 = new Intent(getApplicationContext(), MyReceiver.class); 
          intent3.putExtra("position2", position); 


          PendingIntent pendingIntent3 = PendingIntent.getBroadcast(getApplicationContext(), 105, intent3, PendingIntent.FLAG_UPDATE_CURRENT); 

          AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
          alarmManager.set(AlarmManager.RTC, calendar3.getTimeInMillis(), pendingIntent3); 
         } 


         if (position == 3) { 

          Calendar calendar4 = Calendar.getInstance(); 

          calendar4.set(Calendar.MONTH, 6); 
          calendar4.set(Calendar.DAY_OF_MONTH, 20); 

          calendar4.set(Calendar.HOUR_OF_DAY, 21); 
          calendar4.set(Calendar.MINUTE, 5); 
          calendar4.set(Calendar.SECOND, 0); 


          Intent intent4 = new Intent(getApplicationContext(), MyReceiver.class); 
          intent4.putExtra("position3", position); 


          PendingIntent pendingIntent4 = PendingIntent.getBroadcast(getApplicationContext(), 102, intent4, PendingIntent.FLAG_UPDATE_CURRENT); 

          AlarmManager alarmManager4 = (AlarmManager) getSystemService(ALARM_SERVICE); 
          alarmManager4.set(AlarmManager.RTC, calendar4.getTimeInMillis(), pendingIntent4); 
         } 
         dialog.dismiss(); 
        } 
       }); 

       alert.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         dialog.dismiss(); 
        } 
       }); 
       alert.show(); 
      } 
     }); 
    } 
} 


private class myListAdapter extends ArrayAdapter<list> { 
    public myListAdapter() { 
     super(BuddhaEvent.this, R.layout.custom_events_layout, myList); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View itemView = convertView; 
     if (itemView == null) { 
      itemView = getLayoutInflater().inflate(R.layout.custom_events_layout, parent, false); 
     } 
     list current_list = myList.get(position); 


     ImageView imageView = (ImageView) itemView.findViewById(R.id.id_image); 
     imageView.setImageResource(current_list.getIconId()); 


     TextView titleTxt = (TextView) itemView.findViewById(R.id.id_event); 
     titleTxt.setText(current_list.getName()); 

     EditText text = (EditText) itemView.findViewById(R.id.id_desp); 
     text.setText(current_list.getDetails()); 


     TextView date = (TextView) itemView.findViewById(R.id.id_date); 
     date.setText(current_list.getDate()); 

     return itemView; 
    } 


} 


public void onClick(View v) { 
    startActivity(new Intent(BuddhaEvent.this, BuddhaScreen.class)); 
} 

} ` これは、放送受信機

public class MyReceiver extends BroadcastReceiver { 


@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 


    if(bundle==null){ 
     return; 
    }else{ 
     if(bundle.getInt("position0")==0){ 
      Log.d("INSIDE", "POSITION0 : " + bundle.getInt("position0")); 

      Utils utils = new Utils(); 
      utils.generateNotification(context); 
     } 

     if(bundle.getInt("position1")==1){ 
      Log.d("INSIDE", "POSITION1: " + bundle.getInt("position1")); 

      Utils utils = new Utils(); 
      utils.generateNotification2(context); 
     } 

     if(bundle.getInt("position2")==2){ 
      Log.d("INSIDE", "POSITION2 : " + bundle.getInt("position2")); 

      Utils utils = new Utils(); 
      utils.generateNotification3(context); 
     } 

     if(bundle.getInt("position3")==3){ 
      Log.d("INSIDE", "POSITION3 : " + bundle.getInt("position3")); 

      Utils utils = new Utils(); 
      utils.generateNotification4(context); 
     } 
    } 


} 

}

これは、通知のトラック

public class Utils { 


public void generateNotification(Context context) { 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    Intent repeating_intent = new Intent(context, BuddhaNow.class); 
    // replace the old activity if there is a case that it is already opened 
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT); 


    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setWhen(System.currentTimeMillis()) 
      .setContentTitle("Notification Title1") 
      .setContentText("Notification Text1") 
      .setAutoCancel(true); 

    notificationManager.notify(100, builder.build()); 


} 


public void generateNotification2(Context context) { 

    NotificationManager notificationManager2 = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    Intent repeating_intent2 = new Intent(context, BuddhaNow.class); 
    // replace the old activity if there is a case that it is already opened 
    repeating_intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 101, repeating_intent2, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent2) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setWhen(System.currentTimeMillis()) 

      .setContentTitle("Notification Title2") 
      .setContentText("Notification Text2") 
      .setAutoCancel(true); 

    notificationManager2.notify(101, builder2.build()); 


} 

public void generateNotification3(Context context) { 

    NotificationManager notificationManager3 = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    Intent repeating_intent3 = new Intent(context, BuddhaNow.class); 
    // replace the old activity if there is a case that it is already opened 
    repeating_intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 102, repeating_intent3, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder3 = new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent3) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setWhen(System.currentTimeMillis()) 

      .setContentTitle("Notification Title3") 
      .setContentText("Notification Text3") 
      .setAutoCancel(true); 

    notificationManager3.notify(102, builder3.build()); 


} 

public void generateNotification4(Context context) { 

    NotificationManager notificationManager4 = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    Intent repeating_intent4 = new Intent(context, BuddhaNow.class); 
    // replace the old activity if there is a case that it is already opened 
    repeating_intent4.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent4 = PendingIntent.getActivity(context, 105, repeating_intent4, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder4 = new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent4) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setWhen(System.currentTimeMillis()) 

      .setContentTitle("Notification Title4") 
      .setContentText("Notification Text4") 
      .setAutoCancel(true); 

    notificationManager4.notify(105, builder4.build()); 


} 

} 
+0

あるRTC 。それらをRTC_WAKEUPに変更してみてください。理由はRTC_WAKEUPがデバイスを復帰させ、保留中のインテントを配信するためです。一方、RTCは、デバイスが起動するときにのみインテントを配信します。 –

+0

@VenkateshGoudおかげさまで再生しました。私はすでにRTC_WAKEUPと交換しましたが、まだ動作しません。 – timmy24565

+0

BroadcastReceiverの代わりにWakefulBroadcastReceiverを拡張してください。 –

答えて

0

あなたを保つ私のutilsのクラスでの拡張MyReceiverクラスでありますsetExactを使用してください警報の発射の正確な時間を記録するためにthod。

しかし、主な問題は、バンドルにはposition0が原因で、存在しない場合にbundle.getInt("position0")==0は常にtrueになるということです。 のマッピングが必要ない場合

が与えられたキー、または0に関連付けられた値を返します。指定されたキーの型が存在します。

intent.putExtra("position", position); 

をし、その内部の放送受信機のようにそれを確認します:

ので、それを修正するために、位置を渡す

ので位置2&3つのアラームタイプの
int position = bundle.getInt("position"); 

switch (position): 
    case 0: 
     utils.generateNotification(context); 
     break; 
    case 1: 
     utils.generateNotification1(context); 
     break; 
    ... 
+0

ガイドラインに従っても問題なく動作します。今、デバイスを起動またはシャットダウンしても通知するように、アラームを設定する方法を教えてください。電話機が再起動すると、すべての通知を追跡できるはずです。 これは<受信機のアンドロイド:名= "MyReceiver">私は 'マニフェストファイル にやったことある <意図-フィルタ> <アクションアンドロイド:名= "android.intent.action.BOOT_COMPLETED"> – timmy24565

+0

@ timmy24565もう1つのトピックを作成してください。私の答えで元の質問が解決したら、正解とマークしてください。 – Divers

関連する問題