【问题标题】:How to avoid continuous notification when notification is clicked单击通知时如何避免连续通知
【发布时间】:2016-07-29 14:26:48
【问题描述】:

我想通过 BroadcastReceiver 发送通知。通知由 AlarmReceiver 发送,但在我单击通知时再次发送通知。同样的事情一次又一次地发生,就像死循环一样

这是我的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());

    }
}

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);

    }
}

【问题讨论】:

  • 嗨@emrekose26,您遇到了这个问题,因为您在创建活动时设置了set Alarm(),当您收到通知并单击通知时,MainActivity 已打开。所以,再次调用 setAlarm() 再次发出通知。

标签: java android notifications alarmmanager receiver


【解决方案1】:

点击通知时,它会打开 MainActivity 并在此活动的 onCreate 方法中调用 setAlarm() 。所以点击通知onCreate方法被调用然后setAlarm()被调用,这再次设置警报和构建通知。

进行以下更改

调用按钮的setAlarm()onClick,以便它不会自动调用活动的onCreate

如果您想自动发送通知

改变notification的意图

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

到目前为止,您正在使用 Intent 在点击通知时打开 ManinActivity

Intent更改为

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

OtherActivity 是在onCreate 方法中没有setAlarm() 或构建notification 的活动。

替代方法

使用sharedPreferences 检查通知是否构建一次。 如果构建一次,则不要调用setAlarm()

【讨论】:

  • 通知应该自动发送,因此我不能使用onClick
  • @emrekose26 我已经编辑了我的答案。有任何问题请告诉我
  • 感谢您的回答,但单击通知时不应打开其他活动。
  • @emrekose26 您的MainActivity 是启动器活动吗?
  • @emrekose26 点击通知你想做什么?
猜你喜欢
  • 2012-07-04
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多