2012-09-04 26 views
0

我正在處理將在觸發警報時顯示通知的應用程序。只有當應用程序仍在運行時,通知纔會顯示,但是我希望即使應用程序關閉,通知也會保留,因此當用戶選擇通知時,它將再次運行應用程序。有沒有辦法做到這一點?任何幫助將不勝感激。我也會讚賞提供的任何示例或教程。非常感謝你。確保即使應用程序關閉時通知仍然存在

回答

0

首先,您需要使用服務創建您的通知。我之前也有同樣的問題。我使用phonegap,並從插件創建通知,但事實證明,只有服務可以在後臺運行。然後在服務中添加我的代碼以創建通知,併爲了使用廣播接收器的目的。

 notificationManager = (NotificationManager) context.getSystemService(android.content.ContextWrapper.NOTIFICATION_SERVICE); 
     note = new Notification(imageResource, tickerText, System.currentTimeMillis()); 

    // Intent notificationIntent = new Intent(context, path.to.class); 
     Intent notificationIntent = new Intent(context, package.path.to.receiver.class); 

     notificationIntent.setAction(Intent.ACTION_VIEW); 
     notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

     //contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     note.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

正如您在上面的代碼中看到的,我註釋掉了getActivity,並將其更改爲getBroadcast。在服務中運行意味着您可以在應用程序關閉時收到通知。對於在接收器的意圖,能夠打開你的應用程序如果關閉添加

... 

@Override 
public final void onReceive(Context context, Intent intent) { 
    Log.v('TAG','TEST'); 


    //start activity 
    Intent i = new Intent(); 
    i.setClassName("package.path", "package.path.mainActivity"); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
} 
... 

和XML

<receiver android:name="path.to.receiver" android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="path.to.receiver.BROADCAST" /> 
    </intent-filter> 
</receiver> 

我希望這有助於

+0

嗨,謝謝你的回答,我會在我的應用程序中試用它。如果我有任何問題,我會通知你。 (: – CallMyName

+0

嗨,我已經從[這裏](http://android-er.blogspot.sg/2011/04/start-service-to-send-notification.html)的參考測試了你的代碼。但是當我關閉應用程序並點擊通知,它不會將我重定向迴應用程序。請問您是否可以在此問題上指導我?(PS:我在真實設備上對其進行了測試)。 – CallMyName

+0

我添加了一個缺失的xml文件。解決它 –

3

怎麼樣,我使用

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

我想在未來推通知alarmMenager 它適用於應用程序是stil我正在運行,而我呼叫allarm menager來設置通知,並關閉應用程序我的通知沒有顯示。 我需要做什麼?

這裏你長了我的事件發送:

Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR, HOUR_OF_DAY); 
cal.set(Calendar.MINUTE, MINUTE); 
//cal.add(Calendar.SECOND, SECOND_OF_DAY); 
Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class); 
intent.putExtra("alarm_status", statusMessage); 
intent.putExtra("alarm_title", title); 
intent.putExtra("alarm_content", content); 
Log.i("SenderEvent ", "przygotowane dane"); 
PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

而且Reciever:

Bundle bundle = intent.getExtras(); 
     String statusMessage = bundle.getString("alarm_status"); 
     String title = bundle.getString("alarm_title"); 
     String content = bundle.getString("alarm_content"); 
     nm = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(), 0); 
Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());; 
     //notif.largeIcon = bitmap; 
     notif.icon =2130837504; 
     notif.tickerText=statusMessage; 
     notif.when= System.currentTimeMillis(); 
     /* 
     new Notification(0, 
       statusMessage, System.currentTimeMillis());*/ 
     notif.setLatestEventInfo(context, title, content, contentIntent); 
     nm.notify(NOTIFY_ME_ID, notif); 

哪些錯誤與推通知,並在未來,而應用程序是關門?

相關問題