SoulRayder
SoulRayder

Reputation: 5166

Detecting arrival of push notifications in android app

Is it possible to detect the arrival of push notifications programmatically on the android app? How should one proceed to implement the same?

Upvotes: 2

Views: 2074

Answers (1)

droidx
droidx

Reputation: 2172

As @drees suggested in the comment, you can create a custom broadcastreceiver that extends ParsePushBroadcastReceiver.

Like this:

public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {

// Sample code
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            final String notificationTitle = json.getString("title").toString();
            final String notificationContent = json.getString("alert").toString();
            final String uri = json.getString("uri");

//Create a taskstack builder - this is just sample(incomplete) to give an idea
            Intent resultIntent = null;
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

            // Customize your notification
            NotificationCompat.Builder builder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.mipmap.ic_notification_icon)
                            .setContentTitle(notificationTitle)
                            .setContentText(notificationContent)
                            .setGroup(GROUP_SHORTR_NOTIFS)
                            .setContentIntent(resultPendingIntent)
                            .setAutoCancel(true)
                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                            .setDefaults(Notification.DEFAULT_VIBRATE)
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(notificationContent));

            int mNotificationId = 001;
            NotificationManager mNotifyMgr =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, builder.build());


        } catch (JSONException e) {
            Log.d(TAG, e.getMessage());
        }

    }
}

Add the following in the manifest.

 <receiver
            android:name=".receivers.ParseCustomBroadcastReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>

If you are following this tutorial then the above manifest edit just requires you to change the android:name property.

Hope this helps.

Upvotes: 2

Related Questions