最近,我一直在仔细研究这两个问题和解决方案,试图获得多个通知,每个通知都有自己的活动,从他们自己的相应通知中获取String附加功能。
How to pass text from notification to another activity? How to send parameters from a notification-click to an activity?
在大多数情况下,当前的解决方案除了一个关键问题外有效: 当应用程序未打开时,我收到两个不同的通知。我单击打开通知详细信息活动,活动更新中的文本就可以了。但是,在保持相同活动的同时,我点击第二个通知,没有任何反应。
当应用程序打开时,此解决方案正常工作。它打开两个单独的意图,具有两个不同的通知文本视图。如果应用程序已关闭,我退出第一个通知视图详细信息活动,然后打开第二个,它也可以很好地完成解决方案。
非常感谢任何帮助!
这是我的代码:
MainActivity类
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void setAlarm1(View view){
Toast.makeText(getApplicationContext(),"Setting Alarm 5 seconds",Toast.LENGTH_SHORT).show();
//this is 5 seconds
long alertTime = new GregorianCalendar().getTimeInMillis();
alertTime += 5*1000;
Intent alertIntent = new Intent(this,AlertReceiver.class);
alertIntent.putExtra("msg","This is msg for 5 seconds");
alertIntent.putExtra("msgText","This is msgText for 5 seconds");
alertIntent.putExtra("msgAlert","This is msgAlert for 5 seconds");
alertIntent.putExtra("notifyID",1);
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,alertTime,
PendingIntent.getBroadcast(this,1,alertIntent,
0));
}
public void setAlarm2(View view){
Toast.makeText(getApplicationContext(),"Setting Alarm 10 seconds",Toast.LENGTH_SHORT).show();
//this is 5 seconds
long alertTime = new GregorianCalendar().getTimeInMillis();
alertTime += 10*1000;
Intent alertIntent = new Intent(this,AlertReceiver.class);
alertIntent.putExtra("msg","This is msg for 10 seconds");
alertIntent.putExtra("msgText","This is msgText for 10 seconds");
alertIntent.putExtra("msgAlert","This is msgAlert for 10 seconds");
alertIntent.putExtra("notifyID",2);
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,alertTime,
PendingIntent.getBroadcast(this,2,alertIntent,
0));
}
AlertReceiver类
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, intent.getStringExtra("msg"), intent.getStringExtra("msgText"), intent.getStringExtra("msgAlert"), intent.getIntExtra("notifyID",999));
}
public void createNotification(Context context, String msg, String msgText, String msgAlert, int notifyID){
Intent intent = new Intent(context, MoreInfoNotification.class);
intent.putExtra("msg",msg);
intent.putExtra("msgText",msgText);
intent.putExtra("msgAlert",msgAlert);
// had this below line of code but made problems worse
// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notificIntent = PendingIntent.getActivity(context, notifyID,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
style.setBigContentTitle(msg);
style.bigText(msgText);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(msg);
builder.setContentText(msgText);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker(msgAlert);
builder.setStyle(style);
builder.setContentIntent(notificIntent);
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
builder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID,builder.build());
}
}
MoreInfoNotification类
public class MoreInfoNotification extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = getIntent().getExtras();
if(extras != null){
setContentView(R.layout.activity_more_info_notification);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mTextView = (TextView)findViewById(R.id.textView);
String totalString = "";
if(extras.containsKey("msg")){
totalString += extras.getString("msg");
}
if(extras.containsKey("msgText")){
totalString += ", " + extras.getString("msgText");
}
if(extras.containsKey("msgAlert")){
totalString += ", " + extras.getString("msgAlert");
}
mTextView.setText(totalString);
}
}
}
答案 0 :(得分:0)
所以我最近找到了这个问题的答案。意图被视为相同而不是分开,这正是我所寻求的。为了使Intents不被视为重复,我写了这一行:
intent.setAction("com.wordpress.zackleaman.materialtablayout.intent.action.ACTION_NAME" + notifyID);
在我的AlertReceiver类中,createNotification方法,就在我将额外内容放入我的意图之后。
我知道最好的方法是使用: PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
with:intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
然而,无论我尝试了多少不同的解决方案,如果我打开第一个通知详细信息并且在应用程序最初关闭时单击以打开第二个通知详细信息,则无法更新文本。