Tôi đang tạo một ứng dụng phải hiển thị nhiều hơn 1 thông báo. Tôi đang tìm kiếm thông tin và tôi sử dụng mã này trong lớp học:Nhiều thông báo và chỉ hiển thị thông báo đầu tiên Android
Notification noti = new NotificationCompat.Builder(this)
Tôi có thể hiển thị nhiều thông báo trên thanh, nhưng vấn đề là khi tôi nhấp vào nó chỉ hiển thị thông báo đầu tiên. Tôi nghĩ rằng nó bởi vì id mà mục đích đang chờ xử lý có được là luôn luôn giống nhau, nhưng tôi gửi một cái khác.
Hoạt động thông báo: Hiển thị thông báo mới khi tôi nhấp vào nút.
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
public class NotificationActivity extends Activity {
int notificationID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_example);
}
public void onClick(View v){
displayNotification();
}
protected void displayNotification(){
Intent i = new Intent(this, NotificationView.class);
i.putExtra("notificationID", notificationID);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
CharSequence ticker ="Display";
CharSequence contentTitle = "Title";
CharSequence contentText = "Description";
Notification noti = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setTicker(ticker)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher, ticker, pendingIntent)
.setVibrate(new long[] {100, 250, 100, 500})
.build();
Log.d("Send Notification", "id: "+notificationID);
nm.notify(notificationID, noti);
notificationID++;
}
}
Như bạn có thể thấy tôi tăng ID thông báo mỗi khi tôi nhấp vào nút để gửi id khác.
Chế độ xem thông báo: Hiển thị id của thông báo.
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;
public class NotificationView extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
txt= (TextView) findViewById(R.id.txt1);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
txt.setText("Notification ID: "+getIntent().getExtras().getInt("notificationID"));
// Cancel notification
nm.cancel(getIntent().getExtras().getInt("notificationID"));
}
}
đây chỉ cho thấy id của thông báo, nhưng nó luôn luôn 1.
Bất kỳ ý tưởng về những gì happend? Tôi đang thử nghiệm nó trên một thiết bị 2.3.
Cảm ơn tất cả.
bạn có thể giải thích nó xin vui lòng? tôi không hiểu những gì bạn cố gắng nói với tôi. --------------------- Ok, nó hoạt động :) cảm ơn. – Fernando
Mã này tạo ra các thông báo khác nhau khác nhau nhưng không ghi đè – AnilPatel
về cơ bản cung cấp cho mỗi lần gửi một số duy nhất ở đây. sau khi sử dụng số đó trong thông báo(), hãy tăng số đó cho thông báo tiếp theo đi kèm. –