2016-03-22 16 views
0

GCM通知を正しく受け取っているアプリを作成しました。私はアプリは通知が鳴っている開いていると、以下のコードで定義されても通知は大きなボックスとして適切に来るとき:アプリが閉じているか、電話がロックされている場合でも、通知が来ているAndroid GCM通知アプリが終了してもビルダー機能が動作しない

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

、電話は振動していない/鳴っている。また、通知は1行のボックス(定義されたBigTextStyleではなく)としてのみ表示されるため、通知メッセージの一部を読み取ることができません。したがって、NotificationCompat.Builderで定義されている機能は、アプリが開いている場合にのみ機能します。私の質問は、アプリケーションが閉じられたときに通知音を鳴らし、BigTextStyleに表示させる方法です。これらの設定は、アプリが開いているときに正しく機能していることに注意してください。

PS。許可は、マニフェストファイルに追加されます。

<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

それは

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

をonReceiveと

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

はなぜuが「android.os.Build(場合が必要なのか、このメソッドを呼び出して置きます。 VERSION.SDK_INT arianit

関連する問題