0
我希望在更新ListView
时通知用户通知。在我的应用中,管理员发布消息,并且应该通知用户有关该消息。我怎样才能做到这一点?如何在ListView在Android中更新时立即通知用户
在Android Docs的帮助下,我能够构建通知,但无法在我的应用中实现它。请指导我。
我用于通知该代码在我的应用程序的活动之一:
mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.bcm_logo)
.setContentTitle("BCM")
.setDefaults(Notification.DEFAULT_SOUND)
.setContentText("You may have a new message")
.setAutoCancel(true); // clear notification when clicked
mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
// As per my requirement, the users should get notified whenever the ListView gets updated. Where should I use this line in my Activity.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
这是我MainActivity
代码:
public class MainActivity extends AppCompatActivity {
...
// [Start of onCreate Method]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// Initialize message ListView and its adapter
List<Message> messages = new ArrayList<>();
mMessageAdapter = new MessageAdapter(this, R.layout.item_message_left_user, messages);
mMessageListView.setAdapter(mMessageAdapter);
...
// NOTIFICATIONS
mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.bcm_logo)
.setContentTitle("BCM")
.setDefaults(Notification.DEFAULT_SOUND)
.setContentText("You may have a new message")
.setAutoCancel(true); // clear notification when clicked
mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
//mNotifyMgr.notify(mNotificationId, mBuilder.build());
} // [End of onCreate Method]
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
Log.d(TAG, "User Signed in! "));
} else if (resultCode == RESULT_CANCELED) {
finish();
}
}
}
// WHAT IF SIGNED IN
private void onSignedInitialize(String userDisplayName) {
mUserName = userDisplayName;
Message.setSenderName(mUserName);
attachDatabaseReadListener();
...
});
}
// ATTACH AND DETACH THE DATABASE READ LISTENERS
private void attachDatabaseReadListener() {
if (childEventListener == null) {
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
mMessageAdapter.add(message);
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
public void onCancelled(DatabaseError databaseError) {
}
};
messagesDatabaseReference.addChildEventListener(childEventListener);
}
}
private void detachDatabaseReadListener() {
if (childEventListener != null) {
messagesDatabaseReference.removeEventListener(childEventListener);
childEventListener = null;
}
}
...
} // [END of Main Activity]