在单击事件时将通知的文本和图像传递给另一个 activity
Passing Notification's Text and image to another activity on click event
首先,我尝试了堆栈溢出中提到的所有答案,但是 none 适用于我的应用程序,我知道这很简单,但我无法弄清楚为什么它不起作用。
所以我看到与您分享我的代码,以帮助我解决这个问题。
我 运行 在 API 22
.
这是 activity
,其中包含 textView
以接收来自通知的文本。
public class EventDetails extends AppCompatActivity {
public static TextView txtDet;
public String strdet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
setContentView(R.layout.activity_event_details);
txtDet = (TextView) findViewById(R.id.txtDetail);
Intent MyIntent = getIntent();
strdet = MyIntent.getStringExtra("txtDetails");
txtDet.setText(strdet);
}
我在点击日历日期时使用 getStringExtra
传递文本并且效果很好,所以我想使用相同的 textView
来接收通知文本,我没有问题使用另一个 textView
,同时我想为每个通知接收一个不同的 image
。
这是通知 activity
,我命名为 AlertReceiver
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getIntExtra("id", -1);
switch(notificationId){
case 1:
createNotification(context, "title1", "event1", "event of today");
break;
case 2:
createNotification(context, "title2", "event2", "event of today");
break;
case 3:
createNotification(context, "title3", "event3", "event of today");
break;
case 4:
createNotification(context, "title4", "event4", "event of today");
break;
}
}
public void createNotification(Context context, String msg, String msgText,String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, EventDetails.class), 0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
有什么建议吗?
在创建通知方法中将数据添加到意图。您刚刚在点击通知时指定了应该调用哪个 activity。
像这样:
PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
new Intent(context, EventDetails.class).putExtra("txtDetails", yourMsg), 0);
首先,我尝试了堆栈溢出中提到的所有答案,但是 none 适用于我的应用程序,我知道这很简单,但我无法弄清楚为什么它不起作用。
所以我看到与您分享我的代码,以帮助我解决这个问题。
我 运行 在 API 22
.
这是 activity
,其中包含 textView
以接收来自通知的文本。
public class EventDetails extends AppCompatActivity {
public static TextView txtDet;
public String strdet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
setContentView(R.layout.activity_event_details);
txtDet = (TextView) findViewById(R.id.txtDetail);
Intent MyIntent = getIntent();
strdet = MyIntent.getStringExtra("txtDetails");
txtDet.setText(strdet);
}
我在点击日历日期时使用 getStringExtra
传递文本并且效果很好,所以我想使用相同的 textView
来接收通知文本,我没有问题使用另一个 textView
,同时我想为每个通知接收一个不同的 image
。
这是通知 activity
,我命名为 AlertReceiver
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getIntExtra("id", -1);
switch(notificationId){
case 1:
createNotification(context, "title1", "event1", "event of today");
break;
case 2:
createNotification(context, "title2", "event2", "event of today");
break;
case 3:
createNotification(context, "title3", "event3", "event of today");
break;
case 4:
createNotification(context, "title4", "event4", "event of today");
break;
}
}
public void createNotification(Context context, String msg, String msgText,String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, EventDetails.class), 0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
有什么建议吗?
在创建通知方法中将数据添加到意图。您刚刚在点击通知时指定了应该调用哪个 activity。
像这样:
PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
new Intent(context, EventDetails.class).putExtra("txtDetails", yourMsg), 0);