如果应用程序关闭,Gcm推送通知触发器徽标获得白色矩形背景

时间:2016-11-16 09:06:57

标签: android push-notification google-cloud-messaging

我正在研究Gcm推送通知。它工作正常。如果应用程序处于关闭状态,我也可以获得通知。我的意思是,如果我当时关闭了应用程序,我也会收到通知。但这里的问题是通知栏中的触发徽标。如果应用程序已打开,我正在获取Trigger徽标。如果应用程序已关闭,我将收到白色矩形背景而不是Notification中的图像。我正在发送截图请看我的通知命名为  汽车和自行车。 enter image description here

我的推送通知代码是:

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));
    }

2 个答案:

答案 0 :(得分:0)

如果您定位Android 5.0+,则无法获得彩色通知图标。您必须使用Silhouette图标。如果您想支持棒棒糖前设备上的彩色图标,请检查this答案

答案 1 :(得分:0)

Google还建议您使用将显示在(白色)notification icon后面的颜色。

所以,我认为更好的解决方案是在应用程序中添加一个轮廓图标,并在设备运行Android操作系统时使用它。

下面的代码实现了Lollipop OS版本以上和以下的Notification Builder:

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"
    }

如果您想支持Android Material图标,请查看以下内容:https://material.io/icons/