Jithish P N
Jithish P N

Reputation: 2160

custom notification

Implemented Custom notification using this tutorial

Problem is getting 2 notifications now. one is default Notification and other is custom Notification.

How to disable default Notification.

code :

 public void showNotificationMessages(String title, String message, Intent intent) {
        int icon = R.mipmap.ic_launcher;
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(icon)
                .setContentTitle("Project")
                .setContentText(""+title+" custom notification")
                .setContentIntent(resultPendingIntent)
                 .setAutoCancel(true);
        int mNotificationId = 001;
        NotificationManager mNotifyMgr =
                (NotificationManager) mContext. getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

Manifest

  <receiver  android:name=".receiver.CustomPushReceiver"
      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" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>

Screenshot

Screenshot

CustomReceiver

@Override
protected void onPushReceive(Context context, Intent intent) {
    super.onPushReceive(context, intent);

    if (intent == null)
        return;

    try {
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

        Log.e(TAG, "Push received: " + json);

        parseIntent = intent;

        parsePushJson(context, json);

    } catch (JSONException e) {
        Log.e(TAG, "Push message json exception: " + e.getMessage());
    }
}
 private void parsePushJson(Context context, JSONObject json) {
    try {

        String title = json.getString("alert");

        Intent resultIntent = new Intent(context, MainActivity.class);
        showNotificationMessage(context, title, "", resultIntent);


    } catch (JSONException e) {
        Log.e(TAG, "Push message json exception: " + e.getMessage());
    }
}
    private void showNotificationMessage(Context context, String title, String message, Intent intent) {

    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessages(title, message, intent);
}

Upvotes: 1

Views: 258

Answers (3)

Harpreet
Harpreet

Reputation: 3070

The following code will help to you to show a custom notification on all android devices.

private static void showNotification(Context context, int notificationId, Uri sound) {
        final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        final Intent starterIntent = new Intent(context, SplashActivity.class);
        final PendingIntent pi = PendingIntent.getActivity(context, 0, starterIntent, 0);

        final String CHANNEL_ID = "UniqueChannelId"; // The id of the channel.
        CharSequence name = context.getString(R.string.app_name);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel androidChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
            androidChannel.enableLights(true);
            // Sets whether notification posted to this channel should vibrate.
            androidChannel.enableVibration(true);
            // Sets the notification light color for notifications posted to this channel
            androidChannel.setLightColor(Color.BLUE);
            // Sets whether notifications posted to this channel appear on the lockscreen or not
            androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(androidChannel);
            }
        }
        NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentText(name)
                .setContentTitle(context.getString(R.string.app_name))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setSmallIcon(R.drawable.ic_anom)
                .setContentIntent(pi)
                .setAutoCancel(true);
        NotificationCompat.Style style = new NotificationCompat.InboxStyle();
        notification.setStyle(style);
        notification.setSound(sound, AudioManager.STREAM_NOTIFICATION);
        notification.setLights(Color.BLUE, 5000, 5000);
        notification.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        Notification build = notification.build();
        if (notificationManager != null) {
            notificationManager.notify(notificationId, notification.build());
        }
    }

Upvotes: 0

pavle
pavle

Reputation: 939

It would be great if you share your CustomReceiver, but from my experience, you should extend GcmReceiver, or any other class that maybe already extend it.

Upvotes: 0

Emin Ayar
Emin Ayar

Reputation: 1106

Probably you are trying to manipulate your Parse Notifications.

So if you change your onPushReceive

@Override
protected void onPushReceive(Context context, Intent intent) {
    try {
    JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

    Log.e(TAG, "Push received: " + json);

    parseIntent = intent;

    parsePushJson(context, json);

} catch (JSONException e) {
    Log.e(TAG, "Push message json exception: " + e.getMessage());
}
    return;
}

You will not be able to get your push notification from parse by removing super.OnPushReceive(), so you will see only your custom push notification

Upvotes: 1

Related Questions