我的应用程序会生成多个单独的通知,例如Notification A, Notification B, Notification C.
等...
如果我点按Notification A
,则会显示A的详细信息,之后如果我打开Notification C or B, the activity still shows Notification A contents only
。如何使其更新以显示相应的通知数据。我为每个通知分配了唯一的密钥。
以下是我生成通知的代码
CharSequence title = title1;
CharSequence description = notes;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this.getApplicationContext(), Google_task_notification_preview.class);
intent.putExtra("CODE", key);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(), key, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
try
{
Drawable d = getResources().getDrawable(ic_launcher);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setLargeIcon(bitmap)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(pendingNotificationIntent)
.setSound(soundUri);
if(priority == 0)
mBuilder.setSmallIcon(R.drawable.ic_low);
else
mBuilder.setSmallIcon(R.drawable.ic_high);
mBuilder.setWhen(timestamp);
Notification notification = mBuilder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
// cancel notification after click
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// show scrolling text on status bar when notification arrives
notification.tickerText = title + "\n" + description;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(key, notification);
}
catch(SecurityException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
答案 0 :(得分:1)
肯定会完美无缺!
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nofiticationexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nofiticationexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.nofiticationexample.NotificationA"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.nofiticationexample.NotificationB"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.nofiticationexample.NotificationC"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
MainActivity:
package com.example.nofiticationexample;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
/***
* generateNotification(Dynamically passing the Class Name)
*/
generateNotification("com.example.nofiticationexample.NotificationB");
generateNotification("com.example.nofiticationexample.NotificationC");
generateNotification("com.example.nofiticationexample.NotificationA");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void generateNotification(String className) {
try {
Log.d("className", className);
long notificationID = Calendar.getInstance().getTimeInMillis();
Class cls = Class.forName(className);
Intent resultIntent = new Intent(getApplicationContext(), cls);
resultIntent.setData(Uri.parse("content://" + notificationID));
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, resultIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Your Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("Your Title !");
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify((int) notificationID, mBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
}
通知类:
package com.example.nofiticationexample;
import android.app.Activity;
import android.os.Bundle;
public class NotificationA extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notify_1);
}
}
答案 1 :(得分:0)
特别感谢Mike M,我通过覆盖以下功能解决了这个问题,
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// TODO Auto-generated method stub
mPrimaryKey = intent.getIntExtra("CODE", defvalue);
}
并在我的通知意图中添加了这些intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |Intent.FLAG_ACTIVITY_CLEAR_TOP);
标记。
在待处理意图中仅添加了PendingIntent.FLAG_UPDATE_CURRENT
标志