Android GCM notification Builder功能在应用程序关闭时不工作

Android GCM notification Builder功能在应用程序关闭时不工作,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我创建了一个正确接收GCM通知的应用程序。当我打开应用程序时,通知会响起来,并且通知会像下面代码中定义的一个大盒子一样发出: NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) // .setLargeIcon(imageBitmap)

我创建了一个正确接收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());
即使应用程序已关闭或手机已锁定,但手机未振动/响铃,也会发出通知。此外,通知仅显示为单行框(不具有定义的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" /> 

将其置于接收状态

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);
        }
ActivityManager am=(ActivityManager)getSystemService(ACTIVITY_服务);
List taskInfo=am.getRunningTasks(1);
ComponentName componentInfo=taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase(“com.example.myapp”)){
updateMyActivity(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());
        }
    }
private void sendNotification(字符串消息){
Intent Intent=新Intent(这是FullMapActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
//int color=getResources().getColor(R.color.my\u notif\u color);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);
/*PendingEvent PendingEvent=PendingEvent.getActivity(此,0*/*请求代码*//*,意图,
悬挂式帐篷(一杆旗帜)*/
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
if(android.os.Build.VERSION.SDK_INT
当代码在“else”中重复时,为什么还需要“if(android.os.Build.VERSION.SDK_INT