2014-03-04 51 views
0

我創建了一個android app using webview如果我的WordPress博客上有任何新帖子,想要在手機上收到新帖子的通知?

我想向我的用戶(誰安裝了我的android應用程序)顯示新博客帖子的通知。

當用戶點擊該通知時,網址應該在webview中打開。

我想這樣的教程。

mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
// Sets an ID for the notification, so it can be updated 
int notifyID = 1; 
mNotifyBuilder = new NotificationCompat.Builder(this) 
    .setContentTitle("New Message") 
    .setContentText("You've received new messages.") 
    .setSmallIcon(R.drawable.ic_notify_status) 
numMessages = 0; 
// Start of a loop that processes data and then notifies the user 
... 
    mNotifyBuilder.setContentText(currentText) 
     .setNumber(++numMessages); 
    // Because the ID remains unchanged, the existing notification is 
    // updated. 
    mNotificationManager.notify(
      notifyID, 
      mNotifyBuilder.build()); 

但我是新手,這是不是在所有工作

也試過這個代碼protected void onCreate(Bundle savedInstanceState) {

Intent notificationIntent = new Intent(context, yourwebviewactivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

Notification noti = new NotificationCompat.Builder(context) 
        .setSmallIcon(icon_small) 
        .setTicker("ticker message") 
        .setLargeIcon(largeIcon) 
        .setWhen(System.currentTimeMillis()) 
        .setContentTitle("title") 
        .setContentText("message") 
        .setContentIntent(contentIntent) 
        //At most three action buttons can be added 
        .setAutoCancel(true).build(); 
notificationManager.notify(notifyID, noti); 
+1

您的應用包含Web視圖活動的權利。 –

+0

是的,我的應用包含我的博客鏈接。通過使用'webview'我在手機上顯示 – Neo

回答

2

您需要設置Notification Intent Activity像:

Intent notificationIntent = new Intent(context, yourwebviewactivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

Notification noti = new NotificationCompat.Builder(context) 
        .setSmallIcon(icon_small) 
        .setTicker("ticker message") 
        .setLargeIcon(largeIcon) 
        .setWhen(System.currentTimeMillis()) 
        .setContentTitle("title") 
        .setContentText("message") 
        .setContentIntent(contentIntent) 
        //At most three action buttons can be added 
        .setAutoCancel(true).build(); 
notificationManager.notify(notifyID, noti); 

不要忘記填寫您的WebView Activitymanifest.xml

欲瞭解更多信息,請訪問:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

'context'未定義:( 給它彈出來使用getBaseContext()?? – Neo

+0

請問你可以分享任何鏈接或教程嗎? – Neo

+0

無法工作:!?/ – Neo