0

I am working on Gcm Push Notification.Its working fine.I am able to get Notifications if app is in closed position also.I mean if i closed the app at that time also i am getting notification.But the problem here is triggered logo in notification bar.If app is opened I am getting Trigger logo properly.If app is closed I am getting white rectangualr background instead of image in Notification.I am sending screenshot please see my notification named as Car and Bike.enter image description here

and my push notification code is:


public class GCMPushReceiverService extends GcmListenerService {

    //This method will be called on every new message received
    String json1;
    final static String GROUP_KEY_EMAILS = "group_key_emails";
    public final static String NEW_PUSH_MESSAGE = "CAB_newmessage";
public static String product_classified_id="";
    int NOTIFICATION_ID = 10;
    Intent intent;
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onMessageReceived(String from, Bundle data) {
        //Getting the message from the bundle

        System.out.println("data:" + data);
        Log.d("data", "data----------------->" + data);
        JSONObject json = new JSONObject();
        Set<String> keys = data.keySet();


        System.out.println("key :" + keys);
        for (String key : keys) {
            try {

                json.put(key, JSONObject.wrap(data.get(key)));

                json1 = json.getString("key1") ;
                product_classified_id=json.getString("classified_id");

                System.out.println("hi Bha:" + json);
            } catch(JSONException e) {
                //Handle exception here
            }
        }

        sendNotification(json1);
    }

    //This method is generating a notification and displaying the notification
    private void sendNotification(String message) {
        Intent intent = new Intent(this, ProductViewActivity.class);

        //String a ="12";

        //intent.putExtra("num", a);
        intent.putExtra("id",product_classified_id);
        sendBroadcast(new Intent("xyz"));

        // startActivity(intent);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        int requestCode = 0;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        int color = 0xffff0000;//.setColor(color)

        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.image)//smallanch
                .setContentText(message);
              Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.image);//.setSmallIcon(R.drawable.launcherr)
              noBuilder.setAutoCancel(true)
                .setLargeIcon(largeIcon)
                .setSound(sound)
                .setContentTitle("Car and Bike")
                .setGroup(GROUP_KEY_EMAILS)
                .setGroupSummary(true)
                .setContentIntent(pendingIntent)
                      .setStyle(new NotificationCompat.BigTextStyle().bigText(message));



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

        notificationManager.notify(0, noBuilder.build()); //0 = ID of notification




        // NOTIFICATION_ID++;
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(NEW_PUSH_MESSAGE));
    }
m.v.n.kalyani
  • 760
  • 7
  • 20
  • Please add the json payload you send to google to trigger a push notification – Tim Nov 16 '16 at 09:11
  • instead of getResources() try calling context.getResources().just a hunch din't try this. – Rushi Ayyappa Nov 16 '16 at 09:12
  • ^ that won't help. A service is a Context. – Tim Nov 16 '16 at 09:16
  • Please can you tell me the device on which you are running the app? Is it above 5.0 lollilpop? – UserName_Untold Nov 16 '16 at 09:30
  • Honor Holly,android 4.4.2.And i am getting same problem in Coolpad mobile also(Android 5 version).I am getting problem in all mobiles – m.v.n.kalyani Nov 16 '16 at 09:33
  • According to the material design the small icon you set should be white in color. If your small icon is of any other color this issue may arise. And i think you need to set another attribute to the NotificationCompatBuilder .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) – UserName_Untold Nov 16 '16 at 09:43
  • I tried in android 4.4 getting trigger logo as large and in android five getting triggered logo as white rectangular background. – m.v.n.kalyani Nov 16 '16 at 10:19
  • and one more thing,i am getting this problem if app is in-active state.In active mode working fine. – m.v.n.kalyani Nov 16 '16 at 10:22

2 Answers2

0

You can't have colored notification icon if you are targeting Android 5.0+. You have to use Silhouette icon. Check this answer if you want to support colored icons on pre lollipop devices

Community
  • 1
  • 1
0

Google also suggest that you use a color that will be displayed behind the (white) notification icon.

So, I think that a better solution is to add a silhouette icon to the app and use it if the device is running Android operating system.

Code below implement Notification Builder for above and below Lollipop OS version:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder.setSmallIcon(R.drawable.icon_transperent);
} else { 
    builder.setSmallIcon(R.drawable.icon);
} 
If you want to set colored single icon for all OS version then

Solution: For colored icons, use

defaultConfig {
        applicationId "com.your.app.pkj"
        minSdkVersion xx
        targetSdkVersion 20 // < 21 (target sdk version in manifest file will be less than 21).
        versionCode x
        versionName "x.x"
    }

If you want to support Android Material icons check this: https://material.io/icons/

Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30