7

Thông báo trong android có cùng ý định khi nhấp vào. Tôi đang gửi thông báo sau khi cài đặt chủ đề. Hãy xem xét tôi cài đặt 4 chủ đề và 4 thông báo xuất hiện trong cửa sổ Thông báo, nhưng khi tôi nhấp vào từng thông báo, nó sẽ khởi chạy hoạt động perticular nhưng mục đích là có cùng dữ liệu cho mỗi mục đích.Android Multiple Notification gửi cùng một dữ liệu khi nhấp vào

mã của tôi đi như thế này

@SuppressWarnings("deprecation") 
void sendInstalledNotification(String fileName, String packageName) { 
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 

    String name = ""; 
    try { 
     name += fileName.substring(fileName.lastIndexOf(".") + 1); 
    } catch (Exception e) { 
     Log.e("NewThemeChooser", "Invalid Package name"); 
     e.printStackTrace(); 
    } 
    name += " Installed"; 
    Notification notification = new Notification(R.drawable.ic_launcher_9, name , System.currentTimeMillis()); 

    Intent intent = new Intent(mContext , ThemeInfo.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("apkid", packageName); 
    bundle.putBoolean("isApplied", false); 
    intent.putExtra("bundle", bundle); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); 
    notification.setLatestEventInfo(mContext, name, "Click to Apply Theme", pendingIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    Log.d("NewThemeChooser__:ThemeChangeReceiver" , "hascode : " + packageName.hashCode() + " installed " + packageName); 
    notificationManager.notify(packageName.hashCode(), notification); 

} 

và tôi đang in dữ liệu ý định trong onCreate của ThemeInfo hoạt động như

Bundle bundle = getIntent().getBundleExtra("bundle"); 
    apkid = bundle.getString("apkid"); 
    isApplied = bundle.getBoolean("isApplied", false); 

    System.out.println("NewThemeChooser__:bundle apkid " + apkid); 

Kết quả tôi nhận được trong nhật ký là

D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : -186637114 installed com.test.theme.MiCrease 
D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : 2106806482 installed com.test.theme.iPhone 
D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : -1413669305 installed com.test.theme.Simpsons 
D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : -2146296452 installed com.test.theme.AnnaTheme 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 

Trả lời

18

Tôi có cùng một vấn đề và vấn đề là Android đang quá thông minh và mang lại cho bạn PendingIntent s thay vì mới. Từ docs:

Một sai lầm phổ biến mọi người mắc phải là để tạo ra nhiều PendingIntent đối tượng với Intent s mà chỉ khác nhau về nội dung "thêm" của họ, hy vọng để có được một khác nhau PendingIntent mỗi lần. Điều này không xảy ra. Các bộ phận của Intent được sử dụng để đối sánh là những phần giống nhau được xác định bởi Intent.filterEquals. Nếu bạn sử dụng hai đối tượng Intent tương đương với Intent.filterEquals, thì bạn sẽ nhận được cùng một số PendingIntent cho cả hai đối tượng đó.

Sửa đổi mã của bạn như sau để cung cấp một độc đáo requestCode:

// ... 
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, packageName.hashCode(), intent, 0); 
// ... 

Điều này sẽ đảm bảo rằng một độc đáo PendingIntent được sử dụng, như trái ngược với một tương tự.

Lưu ý rằng hashCode() có thể không phải là duy nhất, vì vậy nếu có thể, hãy sử dụng một số nguyên duy nhất khác là requestCode.

+1

Thực ra tôi đã sửa lỗi này trước đó nhưng không cập nhật. cảm ơn @Oleg –

+0

Wow. tốt đẹp bắt + 1 Upvote. cám ơn vì đã chia sẻ –