Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening activity after clicking push notification android

I am a huge noob to Android programming so sorry if this is a simple task. I pretty much followed the Vogella push notification tutorial for push notifications (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html). I've read some other stack overflow questions but I'm a little confused on how to open a intent once I receive the notification.

For example, if I just wanted the notification to lead me to a website, how would that work? Would it have to go under my MessageReceivedActivity or another project/class all together?

Thanks

Here is the code I have for my C2DMMessageReceiver

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Log.w("C2DM", "Message Receiver called");
    if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
        Log.w("C2DM", "Received message");
        final String payload = intent.getStringExtra("payload");
        Log.d("C2DM", "dmControl: payload = " + payload);
        // TODO Send this to my application server to get the real data
        // Lets make something visible to show that we received the message
        createNotification(context, payload);

    }
}

public void createNotification(Context context, String payload) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "Message received", System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    //adding LED lights to notification
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    Intent intent = new Intent(context, MessageReceivedActivity.class);
    intent.putExtra("payload", payload);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);
    notification.setLatestEventInfo(context, "Message",
            "New message received", pendingIntent);
    notificationManager.notify(0, notification);

}

}

like image 275
Kevin Avatar asked Apr 25 '12 02:04

Kevin


People also ask

Can Google Read push notifications?

Google Can Read Push Notification Content (and How They Affect PushEngage) In this new release of Chrome 86, Google has gone a step further and now will be monitoring the push notification content.


1 Answers

if you want to open a website on notification click try this:

    public void createNotification(Context context, String payload) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "Message received", System.currentTimeMillis());
        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        //adding LED lights to notification
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        Intent intent = new Intent("android.intent.action.VIEW", 
         Uri.parse("http://my.example.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "Message",
                "New message received", pendingIntent);
        notificationManager.notify(0, notification);

    }
like image 166
Zaz Gmy Avatar answered Nov 15 '22 03:11

Zaz Gmy