我想從android狀態欄中刪除一個具有函數cancel()的特定通知。parse.com - 從android上的狀態欄中刪除特定的通知
因爲我需要通知ID。
如何從parse.com通知中獲取此ID?
我想從android狀態欄中刪除一個具有函數cancel()的特定通知。parse.com - 從android上的狀態欄中刪除特定的通知
因爲我需要通知ID。
如何從parse.com通知中獲取此ID?
好吧,這將需要一定量的實驗。基本上你需要一個BroadCaster Parse的自定義類。因此,您需要擴展該功能,並創建自己的自定義類,以便處理自定義事件,例如使用通知構建器進行通知。
編輯:替換完整的代碼。這是行得通的。
public class MyCustomReceiver extends ParsePushBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String payload=intent.getExtras().toString();
Log.d("PARSEPUSH", payload);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent piIntent=PendingIntent.getActivity(context,0,
notificationIntent,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("My Notification").setSmallIcon(android.R.drawable.star_on)
.setContentText("Message Body").setAutoCancel(true);
mBuilder.setContentIntent(piIntent);
Notification pnNotif = mBuilder.build();
Integer notificationId=0;
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, pnNotif); // Set notification ID
}
@Override
protected void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
}
public MyCustomReceiver() {
super();
}
@Override
protected Notification getNotification(Context context, Intent intent) {
return null;
}
}
請確保您更新清單並用MyCustomReceiver替換ParsePushBroadcastReveiver。
對不起,一行沒有得到複製。我忘了提及通知使用哪個圖標。它現在來了..使用intent.getExtras.toString()來讀取在推送通知中發送的有效載荷。將其視爲json並從中檢索消息有效內容。 –
我試過這段代碼,但是當我這樣做時,我沒有在satus欄上得到通知。我在哪裏設置ID?例如我需要這樣的事情: - notificationManager.notify(MY_NOTIFICATION_ID,通知); –
通知來自狀態欄。這對我來說工作得很好。檢查兩件事。一個確保你看到一個Log.d輸出。這至少會表明這個廣播機構正在被呼叫。其次確保您的清單已正確聲明和更新。查看最後一行mNotificationManager.notify(notificationId,pnNotif); 。 notificationId是您的自定義ID。只需用MY_NOTIFICAION_ID替換即可。請記住,這隻能是一個整數值。 –
您可以先發布您的自定義BroadCastReceiverClass的代碼嗎? –
我不使用BroadCastReceiverClass。 –
在BroadCastRecieverClass中設置通知ID。現在,默認情況下,解析會在您的應用程序中實現自己的類,但要控制通知的ID,意圖和其他詳細信息,您必須首先進行設置。 –