0

I am building a chat application for a school project. Everything works fine so far. So i am able to get push notifications through GCM. But those are only shown silently. Can you help me to figure out how to make them with sound (default sound is good enough) and with my own app icon?

EDIT: There is no possibility to use Firebase. We must use GCM for the project.

Below you can see my code to handle the notifications. Thanks.

    NotificationHandler notificationHandler = new NotificationHandler(getApplicationContext());

    //If the app is in foreground
    if (!NotificationHandler.isAppIsInBackground(getApplicationContext())) {
        //Sending a broadcast to the chatroom to add the new message
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
    } else {
        //If app is not in foreground displaying push notification
        notificationHandler.showNotificationMessage(title, message);
    }
hab55
  • 3
  • 4
  • try https://stackoverflow.com/questions/2618182/how-to-play-ringtone-alarm-sound-in-android or https://stackoverflow.com/questions/10335057/play-notification-default-sound-only-android – EugenUngurean Jun 26 '17 at 12:06
  • 1
    suggestion : use FCM and notification builder to build to build notification – Shubham AgaRwal Jun 26 '17 at 12:12
  • @Shubham i should have mentioned that i must use GCM because the school server only works with that. Thanks for your suggestion – hab55 Jun 26 '17 at 12:21
  • by following link you can get code of vibration, sound, light and icon in notification......... https://stackoverflow.com/questions/44569808/notification-icon-showing-white-on-lollipop-and-above-devices/44570141#44570141 – Aniruddh Parihar Jun 26 '17 at 12:23
  • Could you add a sample payload you're sending? – AL. Jun 27 '17 at 14:19

1 Answers1

0

try this one, it works for me

protected void displayNotification(Context context, String[] strContent) {
        try {
            int numMessages = 0;

            /* Invoking the default notification service */
            NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

            mBuilder.setContentTitle("New "+strContent[0].trim());
            mBuilder.setContentText("You've received new "+strContent[0].trim());
            mBuilder.setTicker("New "+strContent[0].trim()+" Alert!");
            mBuilder.setSmallIcon(R.drawable.ic_notif);
            mBuilder.setAutoCancel(true);
            mBuilder.setColor(context.getResources().getColor(R.color.ColorPrimary));
            mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);
            mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});         

            mBuilder.setLights(Color.RED, 3000, 3000);
         mBuilder.setNumber(++numMessages);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            String[] events = new String[strContent.length-1];
            for(int i=0; i<strContent.length-1; i++){
                events[i] = new String(strContent[i+1].trim());
            }

            inboxStyle.setBigContentTitle("New "+strContent[0].trim()+" received!");

            for (int i=0; i < events.length; i++) {
                inboxStyle.addLine(events[i]);
            }

            mBuilder.setStyle(inboxStyle);

            /* Creates an explicit intent for an Activity in your app */
            Intent resultIntent = new Intent(this, HomeActivity.class);


            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(HomeActivity.class);

            /* Adds the Intent that starts the Activity to the top of the stack */
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            /* notificationID allows you to update the notification later on. */
            mNotificationManager.notify(9999, mBuilder.build());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39