ListView
が更新されるたびに通知をユーザーに通知します。私のアプリでは、管理者がメッセージを投稿し、そのメッセージについてユーザーに通知する必要があります。どうやってやるの?ListViewがAndroidで更新されるとすぐにユーザーに通知する方法
Androidのヘルプを使用して通知を作成できましたが、アプリで実装できませんでした。私を案内してください。
私は私のアプリでの活動の一つに、通知のためにこのコードを使用:
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]