如何在Android中更新ListView后立即通知用户

时间:2017-07-03 19:21:46

标签: android notifications android-notifications

我想在ListView更新时通知用户。在我的应用程序中,管理员发布消息,并且应该通知用户该消息。我怎么能这样做?

在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]

1 个答案:

答案 0 :(得分:1)

您可以使用developer docs

void registerDataSetObserver(DataSetObserver观察者)

注册在此适配器使用的数据发生更改时调用的观察者。

参数 observer DataSetObserver:数据集更改时收到通知的对象。 unregisterDataSetObserver

void unregisterDataSetObserver(DataSetObserver observer)

取消注册之前已通过registerDataSetObserver(DataSetObserver)向此适配器注册的观察者。

参数 observer DataSetObserver:要注销的对象。