Android GCM通知Builder功能在应用程序关闭时不起作用 [英] Android GCM notification Builder features not working when app is closed

查看:100
本文介绍了Android GCM通知Builder功能在应用程序关闭时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个正确接收GCM通知的应用程序。当我打开应用程序时,通知正在响起,并且通知正常显示为下面代码中定义的大框:

I have created an app that is receiving GCM notifications correctly. When I have the app open the notification is ringing and also the notification comes properly as a big box as defined in the code below:

 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中。请注意,这些设置在应用程序打开时正常工作。

The notification is coming even when the app is closed or the phone is locked, but the phone is not vibrating/ringing. Also the notification appears only as a one line box (not with the BigTextStyle as defined), so a part of the notification message can't be read. So the features defined in the NotificationCompat.Builder are working only if the app is open. My question is how to make the notification ring when the app is closed, and also make it appear in BigTextStyle. Note that these settings are working properly when the app is open.

PS。权限被添加到清单文件中:

PS. the permissions are added in the manifest file:

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

并调用此方法

and call this method

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

这篇关于Android GCM通知Builder功能在应用程序关闭时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆