1

大家好, 我是新來的android開發,目前在谷歌雲消息工作一切工作正常,但問題是,我無法獲得通知圖標和應用程序鎖定屏幕上的應用程序定義像其他應用程序給出像什麼是應用程序綠色和圖標顯示在鎖定屏幕上類似gmail的紅色和圖標顯示在主屏幕上,所以任何人都可以告訴我如何實現它我非常感謝你。如何在屏幕鎖定時通知推送通知?

private void sendNotification(String msg) { 
     Log.d(TAG, "Preparing to send notification...: " + msg); 
     mNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("GCM Notification") 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
       .setContentText(msg) 
       .setColor(
         getResources().getColor(
           R.color.abc_primary_text_material_dark)) 
       .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) 
       .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 
       .setContentInfo("hi") 
       .setTicker("hi") 
       .setPriority(Notification.PRIORITY_HIGH) 
       .setVisibility(Notification.VISIBILITY_PUBLIC) 
       .setLights(0x0000FF, 1000, 4000) 
       ; 

     PowerManager pm = (PowerManager) getApplicationContext() 
       .getSystemService(Context.POWER_SERVICE); 
     WakeLock wakeLock = pm 
       .newWakeLock(
         (PowerManager.SCREEN_BRIGHT_WAKE_LOCK 
           | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), 
         "TAG"); 
     wakeLock.acquire(15000); 
     // Settings.System.putString(getApplicationContext().getContentResolver(), 
     // Settings.System.NEXT_ALARM_FORMATTED, "hi"); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
     Log.d(TAG, "Notification sent successfully."); 
    } 
+0

請提供您的嘗試代碼。 – Dan

+0

均勻燈不工作。 – androidLover

回答

0
Notification notif = new Notification(
       R.drawable.ic_launcher, message, 
       System.currentTimeMillis()); 
     notif.flags = Notification.FLAG_AUTO_CANCEL; 
     notif.defaults |= Notification.DEFAULT_SOUND; 
     notif.defaults |= Notification.DEFAULT_VIBRATE; 

     Intent notificationIntent = new Intent(context, WelcomeActivity.class); 
     notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 

     notif.setLatestEventInfo(context, title, message, contentIntent); 
0

在你的代碼,它是通過setSmallIcon設置圖標。代碼中的值R.drawable.ic_launcher指的是Android項目的res/drawable目錄中的圖像。只要該圖像已放置在可繪製文件夾中,就可以將其更改爲R.drawable.<name of the icon you want to use>