PendingIntent는 첫 번째 알림에 대해서는 올바르게 작동하지만 나머지 알림에는 올바르지 않습니다.
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Upload", response, pendingIntent);
nManager.notify((int)System.currentTimeMillis(), notification);
}
이 함수는 여러 번 호출됩니다. notification
클릭하면 각각 testActivity를 시작하고 싶습니다 . 불행히도 첫 번째 알림 만 testActivity를 시작합니다. 나머지를 클릭하면 알림 창이 최소화됩니다.
추가 정보 : 함수 displayNotification()
는라는 클래스에 UploadManager
있습니다. 인스턴스화하는 에서 Context
전달됩니다 . 에서 실행되는 UploadManager에서도 함수에서 함수 가 여러 번 호출 됩니다.UploadManager
activity
displayNotification()
AsyncTask
편집 1 : 내가로 문자열 응답을 전달하고 있음을 언급하는 것을 잊었다 Intent intent
int로서 extra
.
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
intent.putExtra("response", response);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
알림이 생성되었을 때 String 응답이 무엇인지 반영하기 위해 추가 "응답"이 필요하기 때문에 이것은 큰 차이를 만듭니다. 대신을 사용 PendingIntent.FLAG_UPDATE_CURRENT
하면 추가 "응답"이에 대한 마지막 호출에서 문자열 응답이 무엇인지 반영합니다 displayNotification()
.
나는 이것이에 대한 문서를 읽은 이유를 알고 FLAG_UPDATE_CURRENT
있습니다. 그러나 현재로서는 어떻게 해결해야할지 모르겠습니다.
사용하지 마십시오 Intent.FLAG_ACTIVITY_NEW_TASK
PendingIntent.getActivity, 사용에 대한 FLAG_ONE_SHOT 대신
댓글에서 복사 :
그런 다음 의도에 더미 동작을 설정하십시오. 그렇지 않으면 추가 항목이 삭제됩니다. 예를 들면
intent.setAction(Long.toString(System.currentTimeMillis()))
고민 한 RemoteViews
여러 가지 Intents
각각 Button
의 HomeScreen
위젯. 다음을 추가하면 작동했습니다.
1. intent.setAction(Long.toString(System.currentTimeMillis()));
2. PendingIntent.FLAG_UPDATE_CURRENT
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(context, MyOwnActivity.class);
intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value");
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(my_button_r_id_received_in_parameter, pendingIntent);
Set Action이 문제를 해결했습니다. 상황에 대한 나의 이해는 다음과 같습니다.
PendingIntent가 각각 첨부 된 여러 위젯이 있습니다. 하나가 업데이트 될 때마다 모두 업데이트되었습니다. Flags는 정확히 동일한 PendingIntents에서 발생하는 일을 설명하기 위해 있습니다.
FLAG_UPDATE_CURRENT 설명은 이제 훨씬 더 잘 읽 힙니다.
동일한 PendingIntent가 이미 존재하는 경우 기존의 모든 PendingIntent를 작성중인 새 PendingIntent로 업데이트하십시오.
똑같은 정의는 엑스트라를 제외하고 전체 PendingIntent를 봅니다. 따라서 각 인 텐트에 다른 추가 기능이 있더라도 (저를 위해 appWidgetId를 추가했습니다) Android에 동일합니다.
Adding .setAction with some dummy unique string tells the OS. These are completely different and don't update anything. In the end here's my implementation which works as I wanted, where each Widget has its own configuration Intent attached:
Intent configureIntent = new Intent(context, ActivityPreferences.class);
configureIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
configureIntent.setAction("dummy_unique_action_identifyer" + appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configureIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
UPDATE
Even better solution in case you're working with broadcasts. Unique PendingIntents are also defined by unique request codes. Here's my solution:
//Weee, magic number, just want it to be positive nextInt(int r) means between 0 and r
int dummyuniqueInt = new Random().nextInt(543254);
PendingIntent pendingClearScreenIntent = PendingIntent.getBroadcast(context,
dummyuniqueInt, clearScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
I see answers but no explanations. Also none of the answers address all possible solutions, so I'll try to make that clear.
Documentation:
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).
Cause of the problem:
You create 2 notifications with 2 pending intents. Each pending intent is associated with an intent:
Intent intent = new Intent(context, testActivity.class);
However, these 2 intents are equal, therefore when your 2nd notification arrives it will launch the first intent.
Solution:
You have to make each intent unique, so that no pending intents will ever be equal. How do you make the intents unique? Not by the extras you put with putExtra()
. Even if the extras are different, the intents might still be equal. To make each intent unique, you must set a unique value to the intent action, or data, or type, or class, or category, or request code: (any of those will work)
- action:
intent.setAction(...)
- data:
intent.setData(...)
- type:
intent.setType(...)
- class:
intent.setClass(...)
- category:
intent.addCategory(...)
- request code:
PendingIntent.getActivity(context, YOUR_UNIQUE_CODE, intent, Intent.FLAG_ONE_SHOT);
Note: Setting a unique request code might be tricky because you need an int, while System.currentTimeMillis()
returns long, which means that some digits will be removed. Therefore I would recommend to either go with the category or the action and setting a unique string.
I had the same problem, and was able to fix it by changing the flag to:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
As documentation said use unique request code:
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).
Fwiw, I have had better luck with PendingIntent.FLAG_CANCEL_CURRENT
than with PendingIntent.FLAG_UPDATE_CURRENT
.
I had the same problem and i fixed it by the below steps
1) Clear any flag for intent
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
2) insert intent.setAction by the below code
intent.setAction(Long.toString(System.currentTimeMillis()));
3) for Pendingintent ,insert the below code
PendingIntent Pintent = PendingIntent.getActivity(ctx,0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
I hope to work with you
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
In PendingIntent is two int parameters, second one and the last one. Second one is "request code" and it must be unicue number (for example id of your notifiction), else if (as in your example it equals zero, it always will be overwritten).
// Use pending Intent and also use unique id for display notification....
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) sqlitewraper.context.getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(id, builder.build());
to send data extra correctly you should send with pending intent the notification id like this: PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
I have the same problem, and use the PendingIntent.html.FLAG_UPDATE_CURRENT to fix it.
I have checked the source code. In ActivityManagerService.java, the key method is as follows. When the flag is PendingIntent.FLAG_UPDATE_CURRENT and the updateCurrent is true. Some extras will be replaced by new and we will get an replaced PendingIntent.
IIntentSender getIntentSenderLocked(int type, String packageName,
int callingUid, int userId, IBinder token, String resultWho,
int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
Bundle bOptions) {
// ... omitted
final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
|PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntentRecord.Key key = new PendingIntentRecord.Key(
type, packageName, activity, resultWho,
requestCode, intents, resolvedTypes, flags, bOptions, userId);
WeakReference<PendingIntentRecord> ref;
ref = mIntentSenderRecords.get(key);
PendingIntentRecord rec = ref != null ? ref.get() : null;
if (rec != null) {
if (!cancelCurrent) {
if (updateCurrent) {
if (rec.key.requestIntent != null) {
rec.key.requestIntent.replaceExtras(intents != null ?
intents[intents.length - 1] : null);
}
if (intents != null) {
intents[intents.length-1] = rec.key.requestIntent;
rec.key.allIntents = intents;
rec.key.allResolvedTypes = resolvedTypes;
} else {
rec.key.allIntents = null;
rec.key.allResolvedTypes = null;
}
}
return rec;
}
rec.canceled = true;
mIntentSenderRecords.remove(key);
}
I had the same problem, and was able to fix it by changing the flag to:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.appointment, null);
AlertDialog.Builder bulider= new AlertDialog.Builder(PatientDetail.this);
final AlertDialog alert=bulider.create();
bulider.setTitle("Enter Date/Time");
bulider.setView(textEntryView);
bulider.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EditText typeText=(EditText) textEntryView.findViewById(R.id.Editdate);
EditText input1 =(EditText) textEntryView.findViewById(R.id.Edittime);
getDateAndTime(typeText.getText().toString(),input1.getText().toString());
}
});
bulider.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
bulider.show();
}
'program story' 카테고리의 다른 글
CSS에 높이가 설정되지 않은 상태에서 div의 높이 가져 오기 (0) | 2020.09.15 |
---|---|
MKMapView 확대 및 영역 (0) | 2020.09.15 |
문자열에서 부동 숫자를 추출하는 방법 (0) | 2020.09.15 |
jQuery 날짜 선택기-과거 날짜 비활성화 (0) | 2020.09.15 |
Google지도 API v2의 기본 위치 및 확대 / 축소 수준을 어떻게 설정합니까? (0) | 2020.09.15 |