我正在尝试启动通知点击活动,但它永远不会开始。以下是我发送通知的代码:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("Notification", type);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(type)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
点击通知我从未接到过该活动的电话。
答案 0 :(得分:1)
尝试这一个 将
中注册此课程Splash_Activity.class
替换为您的活动,并确保您已在Manifest.xml
protected void displayNotification(Context context, String[] strContent) {
try {
int numMessages = 0;
/* Invoking the default notification service */
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("New "+strContent[0].trim());
mBuilder.setContentText("You've received new "+strContent[0].trim());
mBuilder.setTicker("New "+strContent[0].trim()+" Alert!");
mBuilder.setSmallIcon(R.drawable.ic_notif);
mBuilder.setAutoCancel(true);
mBuilder.setColor(context.getResources().getColor(R.color.ColorPrimary));
mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});
mBuilder.setLights(Color.RED, 3000, 3000);
mBuilder.setNumber(++numMessages);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[strContent.length-1];
for(int i=0; i<strContent.length-1; i++){
events[i] = new String(strContent[i+1].trim());
}
inboxStyle.setBigContentTitle("New "+strContent[0].trim()+" received!");
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
mBuilder.setStyle(inboxStyle);
/* Creates an explicit intent for an Activity in your app */
Intent resultIntent = new Intent(this, Splash_Activity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Splash_Activity.class);
/* Adds the Intent that starts the Activity to the top of the stack */
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* notificationID allows you to update the notification later on. */
mNotificationManager.notify(9999, mBuilder.build());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
答案 1 :(得分:1)
当您想要通知时,请调用此方法
public void showNotification() {
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, ShowNotificationDetailActivity.class), 0);
Resources r = getResources();
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.main)
.setContentTitle("title")
.setContentText("Welcome")
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
创建一个这样的类,让你的课程进入活动
public class ShowNotificationDetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, Main.class);
startActivity(intent);
}
}