Want to get a notification on mobile of new post i

2019-08-31 02:30发布

I have created a android app using webview.

I want to show my users (who has installed my android app ) a notification of new blog posts.

When user click on that notification Url should be opened in webview.

I tried like this tutorial.

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());

But i'm newbie, this is not at all working

Also tried this code in 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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-31 02:54

You need to set Notification Intent Activity like:

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);

Do not forget to Register your webview Activity in manifest.xml

For more information go to: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

查看更多
登录 后发表回答