通知方法NotificationCompat - 没有错误 - 没有通知

时间:2013-04-09 12:22:34

标签: android notifications

我想收到一个简单的通知。我已经看到很多使用旧API方法的例子,但我想使用更新的方法。我已经从API资源中将这些代码放在一起,并且它运行时没有错误。但是,我没有看到任何通知。我至少需要查看通知,至少,我相信我所阅读的内容。这是我的代码:

public class Login extends Activity {
    public static Context ctx;

public void onCreate(Bundle savedInstanceState) {
    ctx = this;
 }

private static void notice(String msgfrom, String msg) {
    NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(ctx);
    mBuilder.setSmallIcon(R.drawable.ic_launcher);
    mBuilder.setContentTitle("New message from " + msgfrom.toString());
    mBuilder.setContentText(msg);
    mBuilder.build();
}
}

感谢下面的帮助,我修改了我的代码

NotificationCompat.Builder nb =   new NotificationCompat.Builder(ctx);
nb.setSmallIcon(R.drawable.ic_launcher);
nb.setContentTitle("New message from " + msgfrom.toString());
nb.setContentText(msg);
nb.setAutoCancel(true);

Notification notification = nb.build();
NotificationManager NM = (NotificationManager)         
ctx.getSystemService(NOTIFICATION_SERVICE);
NM.notify(0, notification); 

3 个答案:

答案 0 :(得分:2)

enter image description here

如果您想在所有Android设备< = android 4.2中运行通知,那么只需在项目中添加android-support-v4.jar文件,然后

使用以下代码:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// 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);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.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);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

此代码适用于android 2.1至4.2。

有关详情,请查看thisthis链接。

答案 1 :(得分:1)

你错过了最后一步。您正在构建通知,但仍需要按以下方式将其发布到系统:

( (NotificationManager) ctx.getSystemService ( Context.NOTIFICATION_SERVICE ) ).notify ( 100 , mBuilder.build () );

编辑:

通知方法中使用的整数100(我随机选择)是通知的唯一标识符。如果您需要更新或取消通知,则应使用相同的标识符。


注意:

将活动的联系人存储在静态变量中可能会导致内存泄漏! 而不是那样,你可以改变这个:

private static void notice(String msgfrom, String msg)

到这个

private static void notice(Context ctx , String msgfrom, String msg)

因此删除静态变量。

答案 2 :(得分:0)

试试这个

只需在onCreate()

内拨打电话即可
    Public void Player_Notification()
    {
    final String myBlog = "http://android-er.blogspot.com/";
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(R.drawable.ic_launcher,"Audio Track is playing in background!",System.currentTimeMillis());
    Context context = getApplicationContext();

     // here is title
    String notificationTitle = Title;
     // here is sub title
    String notificationText = data; 
    // write your activity name which you want to open when user click on notification
    Intent myIntent = new Intent(this, Activty.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(Activty.this, 0, myIntent,
    Intent.FLAG_ACTIVITY_NEW_TASK);

  // intent will call your activity as you want
   myNotification.defaults |= Notification.DEFAULT_SOUND;
   myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
   myNotification.setLatestEventInfo(context, notificationTitle,
   notificationText, pendingIntent);
   notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

    }