仅当片段可见时才发送通知
Send Notification only if Fragment is visible
我正在制作一个聊天应用程序(只是为了好玩)。我正在使用 Pushy API 在两个用户之间发送消息。按照 pushy.me 站点中提供的教程,推送消息在广播接收器中接收。好吧,这部分工作正常,但现在我正在制作一个像 Whats App 这样的通知系统,当用户不在聊天时它会启动一个通知栏。
我的想法如下:如果聊天片段可见,只需使用 LocalBroadcastManager sendBroadcast 方法更新片段,否则启动通知。
我正在使用以下代码成功完成此操作:
if (!Utility.isAppInBg(context)) {
Intent chatPushNotification = new Intent(Constants.CHAT_PUSH_NOTIFICATION);
chatPushNotification.putExtra("chat", obj.toString());
LocalBroadcastManager.getInstance(context).sendBroadcast(chatPushNotification);
} else {
if (title != null && msg != null) {
NotificationUtil.notify(context, NOTIFICATION_ID, notifyIntent,
URLDecoder.decode(title, "UTF-8"), URLDecoder.decode(msg, "UTF-8"));
}
}
问题是 isAppInBg 方法使用 ActivityManager with getRunningAppProcesses() 方法,这是不鼓励的。有一种方法可以用另一个检查片段是否可见的方法来替换此方法(请记住,此检查是在广播接收器中进行的)?如果没有,有更好的方法吗?
这种方法对我来说效果很好。
public class MyActivity extends Activity {
static boolean active = false;
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
public void onStop() {
super.onStop();
active = false;
}
@Override
public void onPause() {
super.onPause();
active = false;
}
@Override
public void onResume() {
super.onResume();
active = true;
}
并在接收器中
if(!MyActivity.active){
//alert notification
}
else{
//send broadcast
}
我正在制作一个聊天应用程序(只是为了好玩)。我正在使用 Pushy API 在两个用户之间发送消息。按照 pushy.me 站点中提供的教程,推送消息在广播接收器中接收。好吧,这部分工作正常,但现在我正在制作一个像 Whats App 这样的通知系统,当用户不在聊天时它会启动一个通知栏。
我的想法如下:如果聊天片段可见,只需使用 LocalBroadcastManager sendBroadcast 方法更新片段,否则启动通知。
我正在使用以下代码成功完成此操作:
if (!Utility.isAppInBg(context)) {
Intent chatPushNotification = new Intent(Constants.CHAT_PUSH_NOTIFICATION);
chatPushNotification.putExtra("chat", obj.toString());
LocalBroadcastManager.getInstance(context).sendBroadcast(chatPushNotification);
} else {
if (title != null && msg != null) {
NotificationUtil.notify(context, NOTIFICATION_ID, notifyIntent,
URLDecoder.decode(title, "UTF-8"), URLDecoder.decode(msg, "UTF-8"));
}
}
问题是 isAppInBg 方法使用 ActivityManager with getRunningAppProcesses() 方法,这是不鼓励的。有一种方法可以用另一个检查片段是否可见的方法来替换此方法(请记住,此检查是在广播接收器中进行的)?如果没有,有更好的方法吗?
这种方法对我来说效果很好。
public class MyActivity extends Activity {
static boolean active = false;
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
public void onStop() {
super.onStop();
active = false;
}
@Override
public void onPause() {
super.onPause();
active = false;
}
@Override
public void onResume() {
super.onResume();
active = true;
}
并在接收器中
if(!MyActivity.active){
//alert notification
}
else{
//send broadcast
}