我们有一个服务器向App发送通知,App收到的通知使用NotificationListenerService处理。该应用程序将能够在收到通知时 它连接了WiFi。
现在,当应用程序连接到WiFi时,它会正常接收和处理通知,并且每件事情似乎都很好。但是当服务器在应用程序的WiFi上发送“多个通知”通知时 被禁用,似乎应用程序组装或堆叠这些通知,一旦启用WiFi,所有通知将同时执行,这是我们想要的 摆脱。换句话说,假设现在,应用程序的WiFi被禁用,并且服务器发送3个通知,当用户再次打开应用程序的WiFi时,所有三个 通知将调用以下方法
@Override
public void onNotificationPosted(StatusBarNotification sbn)
下面发布的代码显示了如何编码'NotificationListenerService'的方法。
为了解决这个问题,我同步了代码中显示的启动新线程的行,但这仍然无法解决问题。
即使我收到多个/并发通知流,请立即告诉我如何只处理一个通知
注意:我的意思是在我们的App中处理通知是要播放的声音指示/信号到达通知。
码:
public class NLSNotificationTone extends NotificationListenerService {
private final static String TAG = NLSNotificationTone.class.getSimpleName();
private final static int sDurationOfRingTone = 7; //60 seconds
private boolean mNotificationRemoved;
private static boolean TONE_IS_PLAYING = false;
private Handler handler = null;
private Ringtone mRingTone;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "NLSNotificationTone->onCreate", Toast.LENGTH_LONG).show();
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Toast.makeText(getApplicationContext(), (String) msg.obj, Toast.LENGTH_SHORT).show();
}
};
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.w(TAG, "onNotificationPosted");
Message msg = handler.obtainMessage();
msg.obj = "onNotificationPosted";
handler.sendMessage(msg);
mNotificationRemoved = false;
/*when a notification is recieved, a tone should be played, and if-statement below, is, as long as there is a notification received while a sound is playing which belongs to
a previous notification, then do not play sound again. This to prevent two or more notifications' sound to interchange/mix-up*/
synchronized (this) {
if (!NLSNotificationTone.TONE_IS_PLAYING)
new ThreadPlayTone(this, NLSNotificationTone.sDurationOfRingTone).start();
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.w(TAG, "onNotificationRemoved");
NLSNotificationTone.TONE_IS_PLAYING = false;
Message msg = handler.obtainMessage();
msg.obj = "onNotificationRemoved";
handler.sendMessage(msg);
}
private class ThreadPlayTone extends Thread {
private Context mContext = null;
private int mDurationOfRingTone;
private Ringtone mRingTone = null;
public ThreadPlayTone(Context context, int duration) {
this.mContext = context;
this.mDurationOfRingTone = duration;
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
this.mRingTone = RingtoneManager.getRingtone(context, notification);
}
@Override
public void run() {
super.run();
Message msg = handler.obtainMessage();
msg.obj = "ThreadPlayTone started";
handler.sendMessage(msg);
int initTime = (int) TimeUtils.getTSSec();
while (!mNotificationRemoved && (TimeUtils.getTSSec() - initTime) < this.mDurationOfRingTone) {
Log.w(TAG, "ThreadPlayTone->run->while-loop: " + + ((TimeUtils.getTSSec() - initTime) + 1) + "sec");
/**this method seems asynchronous, it returns immediately*/
this.mRingTone.play();
/*we call this to make sure that ringtone is finished, as long as it is playing, then do nothing, or in other words, wait till
* it finishes*/
while(this.mRingTone.isPlaying()) {}
}
NLSNotificationTone.TONE_IS_PLAYING = true;
}
}
}