2016-03-22 118 views
0

我創建了一個正確接收GCM通知的應用程序。當我有應用程序打開通知振鈴,並通知來正確地作爲一個大箱子,如下面的代碼中定義:Android GCM通知Builder功能在應用程序關閉時不起作用

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
      // .setLargeIcon(imageBitmap) 
       .setTicker("CheGroup") 
       .setContentTitle(title) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setContentIntent(pendingIntent) 
       .setStyle(new NotificationCompat.BigTextStyle() 
         .bigText(message)); 

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

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

甚至當應用程序被關閉或手機鎖定的通知來了,但手機沒有振動/振鈴。此外,通知僅出現在單行框中(與定義的BigTextStyle不同),因此通知消息的一部分無法讀取。因此,NotificationCompat.Builder中定義的功能僅在應用程序處於打開狀態時才起作用。我的問題是如何在應用程序關閉時通知響鈴,並使其顯示在BigTextStyle中。請注意,這些設置在應用程序打開時正常工作。

PS。權限在manifest文件中加入:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<permission 
    android:name="es.appbit.chegroup.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="es.appbit.chegroup.permission.C2D_MESSAGE" /> 

回答

0

把它的onReceive

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
    ComponentName componentInfo = taskInfo.get(0).topActivity; 
    if(componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){ 

     updateMyActivity(message); //send broadcast to notify app 



     //Activity Running 
//   Send a broadcast with the intent-filter which you register in your activity 
//   where you want to have the updates 
     } 
     else{ 
      //Activity Not Running 
      //Generate Notification 
      sendNotification(message); 
     } 

,並調用此方法

private void sendNotification(String message) { 
     Intent intent = new Intent(this,FullMapActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
//  int color = getResources().getColor(R.color.my_notif_color); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     /*PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent, 
       PendingIntent.FLAG_ONE_SHOT);*/ 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 



      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.headerlogo) 
        .setContentTitle("hey..! your booking is confirmed") 
        .setContentText(message) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 


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

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

      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.headerlogo) 
        .setContentTitle("hey..! your booking is confirmed") 
        .setContentText(message) 
        .setAutoCancel(true) 
//     .setColor(color) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 


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

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

爲什麼ü需要「如果(android.os.Build。 VERSION.SDK_INT arianit

相關問題