通知不会打开意图

时间:2014-08-20 10:42:15

标签: java android

当我点击弹出的通知时,它不会打开BaseApplication.class。我在这做错了什么?我希望通知打开BaseApplication.class

public static void sendBasicNotification(int minor, int major, String task) {
    Intent notifyIntent = new Intent(currentContext, BaseApplication.class);
    notifyIntent.putExtra("t", task);
    notifyIntent.putExtra("ma", major);
    notifyIntent.putExtra("mi", minor);

    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(currentContext, 0, new Intent[] { notifyIntent },
            PendingIntent.FLAG_UPDATE_CURRENT);

    int notificationID = 21;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(currentContext)
            .setContentText("You have started the task: " + task).setContentTitle("Task Alert")
            .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true).setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis()).setDefaults(Notification.DEFAULT_ALL);
    Notification notification = builder.getNotification();
    NotificationManager notificationManager = getNotificationManager(currentContext);
    notificationManager.notify(notificationID, notification);
}

static NotificationManager getNotificationManager(Context context) {
    return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}

3 个答案:

答案 0 :(得分:0)

尝试添加此行

notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_SINGLE_TOP);

答案 1 :(得分:0)

原来我把意图发送给了错误的班级。谢谢大家,现在修复。该代码工作正常。

答案 2 :(得分:0)

  

private void sendNotification(String msg){

  Log.d(TAG, "Preparing to send notification...: " + msg);

  mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
     

PendingIntent contentIntent = PendingIntent.getActivity(this,0,                   new Intent(this,MainActivity.class),0);

     

// MainActivity是按意图打开的通知意图

  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Application name")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

  mBuilder.setContentIntent(contentIntent);
  mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

  Log.d(TAG, "Notification sent successfully.");
     

}

希望它能运作