/** Updates the notification. */
void updateNotification(String text) {
Log.i(TAG, text);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, pi);
notificationManager.notify(NOTIFICATION_ID, notification);
}
/**
* Configures service as a foreground service. A foreground service is a service that's doing
* something the user is actively aware of (such as playing music), and must appear to the
* user as a notification. That's why we create the notification here.
*/
void setUpAsForeground(String text) {
/* Intent launchIntent;
String myType="one";
launchIntent = new Intent(getBaseContext(), MainActivity.class);
launchIntent.putExtra("checkme", myType);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
*/
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
notification = new Notification();
notification.tickerText = text;
notification.icon = R.drawable.ic_stat_playing;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, pi);
startForeground(NOTIFICATION_ID, notification);
}
以上代码来自我的服务类,我需要传递一个意图为MainActivity类的字符串。我从Stackover流程中尝试了很多,但没有任何效果。请帮忙 。
答案 0 :(得分:0)
使用此功能。
void updateNotification(String text) {
Log.i(TAG, text);
Intent n = new Intent(getApplicationContext(), MainActivity.class):
n.putExtra("key", yourValue);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
n),
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, pi);
notificationManager.notify(NOTIFICATION_ID, notification);
}
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
PendingIntent contentIntent = null;
String myType="one";
Intent gotoIntent = new Intent();
gotoIntent.setClassName(mContext,"YOURPACKAGENAME.MainActivity");
gotoIntent.putExtra("checkme", myType);
contentIntent = PendingIntent.getActivity(mContext,(int) (Math.random() * 100), gotoIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification = new Notification();
notification.tickerText = text;
notification.icon = R.drawable.ic_stat_playing;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(), "nikhil", text, contentIntent);
startForeground(NOTIFICATION_ID, notification);
答案 2 :(得分:0)
private void updateNotification(String msg) {
Log.d(TAG, "Preparing to update notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class).putExtra("keyName", ValueOfKey), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification update successfully.");
}