2013-04-09 79 views
0

我想獲得一個簡單的通知。我見過很多使用舊的API方法的例子,但我想用新的方法。我已經從API資源中將這些代碼放在一起,並且運行時沒有錯誤。但是,我沒有看到任何通知。我有最低限度的要求查看通知,至少,我相信從我讀過的內容。這裏是我的代碼:通知方法NotificationCompat - 沒有錯誤 - 沒有通知

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); 

回答

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鏈接。

+0

最優秀的..謝謝你...我縮短了我的代碼爲我的情況,但它是你的代碼讓我在那裏! – iBoston 2013-04-09 13:43:07

+0

@ user2250168:歡迎..,接受這個答案,如果它可以幫助你... – 2013-04-09 13:45:50

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); 

    } 
+0

這是不是使用NotificationCompat – iBoston 2013-04-09 13:04:29

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) 

,因此刪除靜態變量。

+0

謝謝,是的,我從'Dhaval Sodha Parmar'的迴應中得出結論。這是一條學習曲線...... – iBoston 2013-04-09 13:54:56

+0

至於你建議不要將我的活動存儲在靜態變量中。好建議 - 我會試試。 – iBoston 2013-04-09 14:03:22