[英]Notification doesn't open activity
I call from service "generateNotification" but after clicking on notification activity doesn't open: 我从服务调用“ generateNotification”,但单击通知活动后无法打开:
from service: generateNotification(getApplicationContext(),contentTitle,contentText,contentText,contentTitle,contentTitle);
来自服务: generateNotification(getApplicationContext(),contentTitle,contentText,contentText,contentTitle,contentTitle);
method in the service: 服务中的方法:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void createNotificationAPI15(Context context, String title, String message, String ticker_message, String summary_message, String big_message) {
int id = (new Random()).nextInt(150) + 1;
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, ListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Notification notif = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.index2)
.setContentIntent(contentIntent)
.setTicker(ticker_message)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(big_message))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.index2))
.setAutoCancel(true)
.build();
mNotificationManager.notify(id, notif);
}
in Manifest 在清单
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
....
<activity
android:name=".ListActivity"
android:exported="true"
android:label="@string/list_act_name" >
</activity>
Create your PendingIntent like this (it works for me): 像这样创建您的PendingIntent(对我有用):
Intent intent = new Intent(context, ListActivity.class);
// optional flags, such as:
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
Make sure you pass your ListActivity.class to intent and not android.app.ListActivity.class! 确保将ListActivity.class传递给intent而不是android.app.ListActivity.class!
Need to make import: 需要输入:
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
Try this. 尝试这个。
Intent notificationIntent = new Intent(yourcurrentclass.this, ListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(yourcurrentclass.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.