android通知不会运行.notify

时间:2014-08-30 14:55:31

标签: android android-notifications

我试图发送一个Android通知,但它在mbuilder.build()部分失败了。我在同一个方法后面有一个警告对话框。我在下面发布了我的代码

  NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.noteiconcon)
    .setContentTitle("Finished search")
    .setContentText("We found your account");
    System.out.println("failed after setText");
  // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);
  System.out.println("failed after result intent");
  // The stack builder object will contain an artificial back stack for the
 // started Activity.
 // This ensures that navigating backward from the Activity leads out of
 // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    System.out.println("taskstackbuilder passed");

  stackBuilder.addParentStack(MainActivity.class);
     System.out.println("stackbuilder.appParentstack passed");

 stackbuilder.addNextIntent(resultIntent);
      System.out.println("stackbuilder.addnextintent passed");
    PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
    0,
    PendingIntent.FLAG_UPDATE_CURRENT
);
    System.out.println("pending intent passed");
mBuilder.setContentIntent(resultPendingIntent);
     System.out.println("mbuilder.setcontentintent passed");
    NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    System.out.println("notification manager passed");
 // mId allows you to update the notification later on.
       mNotificationManager.notify(mId,mBuilder.build());
      System.out.println("notificationManager.notify passed"+ mId);

我已经添加了元数据任务,但日志猫说。

    Bogus static initialization, type 4 in field type Landroid/support/v4/app/NotificationCompat$NotificationCompatImpl; for Landroid/support/v4/app/NotificationCompat; at index 1

就像我说的,我添加了元数据标签,这是在警告对话框之后在asychtask中调用的。我已经注释掉了警告对话框,但它仍然有效。

如果我放弃最后一行。通知它不会崩溃,但显然不会发送通知。

1 个答案:

答案 0 :(得分:0)

我必须使用不同的通知方法,然后接受通知。

       // Set the icon, scrolling text and timestamp
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String MyText = "Finished searching";
    Notification mNotification = new Notification(R.drawable.passicon, MyText, System.currentTimeMillis() );
    //The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears

    String MyNotificationTitle = "Finished Searching";
    String MyNotificationText  = "We can't find your password.";

    Intent MyIntent = new Intent(this, MainActivity.class);
        PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    //A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent

       mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);

    int NOTIFICATION_ID = 1;
    notificationManager.notify(NOTIFICATION_ID , mNotification);  
    //We are passing the notification to the NotificationManager with a unique id.