2016-07-29 1 views
0

Je souhaite envoyer une notification à BroadcastReceiver. Notification envoyée par AlarmReceiver mais à nouveau envoyé une notification lorsque j'ai cliqué sur la notification. La même chose se produit encore, encore et encore comme boucle sans finComment éviter une notification continue lorsque vous cliquez sur la notification

Voici mon AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver { 

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

    android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(context); 

    builder.setSmallIcon(R.mipmap.ic_launcher); 

    // This intent is fired when notification is clicked 
    Intent i = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0); 

    // Notifcation notify sound 
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    // Set the intent that will fire when the user taps the notification. 
    builder.setContentIntent(pendingIntent); 

    // Large icon appears on the left of the notification 
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)); 

    // Content title, which appears in large type at the top of the notification 
    builder.setContentTitle("Have a good weekend"); 

    //Notification click after clear notification 
    builder.setAutoCancel(true); 

    //Set notification sound 
    builder.setSound(alarmSound); 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    // Will display the notification in the notification bar 
    notificationManager.notify(1, builder.build()); 

    } 
} 

et MainActivity.java

public class MainActivity extends AppCompatActivity { 

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

    setAlarm(); 
} 

private void setAlarm(){ 
    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    Intent alarmIntent = new Intent(MainActivity.this, MyAlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 

    calendar.set(Calendar.DAY_OF_WEEK,6); 
    calendar.set(Calendar.HOUR_OF_DAY,16); 
    calendar.set(Calendar.MINUTE,53); 
    calendar.set(Calendar.SECOND,0); 

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent); 

    } 
} 
+1

salut @ emrekose26, vous avez face à ce problème parce que vous définissez Alarm set() en activité et sur la création lorsque vous avez reçu une notification et cliquez sur notification puis MainActivity est ouvert. Par conséquent, appelez à nouveau setAlarm() pour redonner une notification. –

Répondre

0

Comme le clic sur la notification, il ouvre MainActivity et onCreate méthode de cette activité que vous appelez setAlarm(). Donc, au clic de la notification, la méthode onCreate est invoquée, puis setAlarm() est invoquée, ceci à nouveau réglé Alarme et notification de construction.

Do apporteront les modifications suivantes

appel setAlarm()onClick d'un bouton, de sorte qu'il ne soit pas automatiquement invoqué onCreate d'activité.

Si vous souhaitez envoyer une notification automatiquement

changer l'intention de notification

Intent i = new Intent(context, MainActivity.class); 

A partir de maintenant vous utilisez l'intention d'ouvrir ManinActivity sur clic de notification.

changement Intent à

Intent i = new Intent(context, OtherActivity.class); 

OtherActivity est l'activité qui ne setAlarm() ou construire notification dans la méthode onCreate.

Méthode alternative

Utilisez sharedPreferences pour vérifier si la notification est construire une ou non. si la construction une fois alors ne pas appeler setAlarm()

+0

notification devrait envoyer automatiquement donc je ne peux pas utiliser 'onClick' – emrekose26

+0

@ emrekose26 J'ai modifié ma réponse. Faites-moi savoir pour tout problème –

+0

Merci pour votre réponse, mais d'autres activités ne devraient pas ouvrir lorsque la notification cliqué. – emrekose26