如果我的wordpress博客上有任何新帖子,想要在手机上收到新帖子的通知吗?

时间:2014-03-04 13:21:46

标签: android android-layout android-intent webview

我创建了一个android app using webview

  

我想向我的用户(已安装我的Android应用)显示新博客帖子的通知。

     

当用户点击该通知时,应在webview中打开Url。

我试过这个教程。

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 个答案:

答案 0 :(得分: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);

不要忘记在Activity

中注册您的网络视图manifest.xml

有关更多信息,请访问:http://developer.android.com/guide/topics/ui/notifiers/notifications.html