0

我正在尝试获得一个简单的通知。我见过很多使用旧 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); 
4

3 回答 3

2

在此处输入图像描述

如果您想在所有 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。

有关更多详细信息,请查看链接和链接。

于 2013-04-09T12:37:15.840 回答
1

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

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

编辑 :

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


笔记 :

将活动的联系人存储在静态变量中可能会导致内存泄漏!取而代之的是,您可以更改此:

private static void notice(String msgfrom, String msg)

对此

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

因此删除静态变量。

于 2013-04-09T13:43:40.963 回答
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);

    }
于 2013-04-09T12:29:31.887 回答