2

I want to gcm notification come even while app is foreground (on screen). Notification comes when app is closed or background. But when app is on screen (opened) notification does not appear at the top but I need this.

{EDIT: BTW in my code when app is foreground Log.d(TAG, "From: " + from); prints successfully the project number, but Log.d(TAG, "Message: " + message); prints null. (Those lines in onMessageReceived method) And as you know notification does not appear at the top. But when app is closed, those logs never prints but notification appears at the top. I dont understand why logs does not print? }

Here is my code related part. I am using google's github project.

public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";

/**
 * Called when message is received.
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);



    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }

    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    sendNotification(message);
    // [END_EXCLUDE]
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    final Intent notificationIntent = new Intent(this, AccountSummaryActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    Intent intent = new Intent(this, MainMenuActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Bitmap bmURL= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_3_web_3);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(bmURL)
            .setContentTitle("Diversity")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)

            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

Is there any idea how to do it? Thanks in advance.

Hilal
  • 902
  • 2
  • 22
  • 47
  • Could you post piece of code that displays your notification please? Did you check it really triggers with using debugger? – Tieru Jun 07 '16 at 10:57
  • Did you write receiver and services in AndroidManifest? – Naveen Prince P Jun 07 '16 at 11:15
  • Yes I added them. Notification works well when app is not running or background – Hilal Jun 07 '16 at 11:18
  • @Tieru I put Log.e("notification","came"); in sendNotification method and run program, while app is running (foreground) log appeared but notification did not appear at the top. It means it triggers but not appear at the top. Maybe because of app is already running. Is there any example when app is on screen but notification appears top? – Hilal Jun 07 '16 at 12:32
  • did you find any solution – Faisal Naseer May 29 '17 at 08:43
  • @FaisalNaseer If I didn't forget, you need to get json with pure data payload. Then when you get it in onMessageReceived method, send the notification to yourself with notification payload. Then you will see the notification at the top [Check this](https://stackoverflow.com/a/39412257/5528870) – Hilal May 29 '17 at 20:08

0 Answers0