2015-07-03 27 views
0

我試圖安排一段時間後通知出現,但不是顯示通知,而是打開應用程序。計劃的定期通知,但其開放的應用程序

下面的代碼:

 //Build Notification 
     notif.setSmallIcon(R.drawable.ico); 
     notif.setTicker("New Notification!"); 
     notif.setWhen(System.currentTimeMillis()); 
     notif.setContentTitle("Notification Title"); 
     notif.setContentText("Thiis is notification message"); 

     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);; 
     Calendar calendar = Calendar.getInstance(); 
//  calendar.set(Calendar.HOUR_OF_DAY, 12); 
//  calendar.set(Calendar.MINUTE, 00); 
//  calendar.set(Calendar.SECOND, 00); 

     //Intent when clicked 
     Intent i = new Intent(this, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 
     notif.setContentIntent(pendingIntent); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 40* 1000, pendingIntent); 

     //Builds notification and issues 
     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     nm.notify(uniqueId,notif.build()); 

我還添加在清單兩行:

<uses-permission android:name="info.geekstart.www.notificationapp.SET_ALARM"/> 
<reciever android:name=".AlarmReciever"></reciever> 

回答

0

該實施例顯示通知每下午3時天(自Activity內調用):

 Intent myIntent = new Intent(this, NotificationService.class); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 15); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent); 

然後創建服務發佈通知:

public class NotificationService extends Service { 
private NotificationManager mNM; 
private final IBinder mBinder = new ServiceBinder(); 

public NotificationService() { 

} 

public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

/** 
* Class for clients to access 
*/ 
public class ServiceBinder extends Binder { 
    NotificationService getService() { 
     return NotificationService.this; 
    } 
} 

@Override 
public void onCreate() { 
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    showNotification(); 
    return START_NOT_STICKY; 
} 

/** 
* Creates a notification and shows it in the OS drag-down status bar 
*/ 
private void showNotification() { 
     CharSequence title = "Something"; 
     int icon = R.mipmap.ic_launcher; 
     CharSequence text = "Notified message"; 
     long time = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, text, time); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); 
     notification.setLatestEventInfo(this, title, text, contentIntent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     mNM.notify(123456, notification); 
     stopSelf(); 
    } 
}} 

然後顯然是<service android:name=".services.NotificationService" />

相關問題