应用程序未打开时,GCM 消息未发送?

2024-03-25

最近我在我的应用程序上实现了 GCM。我按照这个网站上的教程代码进行操作

http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/ http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/

然而,行为很奇怪,例如

如果我杀死应用程序,那么当我发送通知时,它不会立即显示在通知栏上,而是仅在用户再次打开应用程序时显示,也就是说,如果我在杀死应用程序时发送两个通知,我只会收到两个通知当我打开应用程序时。

这是GCM的行为吗?正如我所期望的,它应该类似于whatsapp(即使我没有打开应用程序,设备仍然应该收到通知并显示)

这是我的代码。感谢您的帮助

Gcm广播接收器

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMNotificationIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

GCMNotificationIntentService

public class GCMNotificationIntentService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GCMNotificationIntentService() {
        super("GcmIntentService");
    }

    public static final String TAG = "GCMNotificationIntentService";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                for (int i = 0; i < 3; i++) {
                    Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }

                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                sendNotification(getResources().getString(R.string.gcm_news_remind));
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    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, SplashScreen.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

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

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.oshpedia"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.example.oshpedia.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <permission
        android:name="com.example.oshpedia.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <application
        android:name=".Defination.MyApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Activity.SplashScreen"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity.Main"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustPan" >
        </activity>
        <activity
            android:name=".Activity.AboutUs"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.AboutApp"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.Disclaimer"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.EnquiryForm"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustPan" >
        </activity>
        <activity
            android:name=".Activity.NewsDetail"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.EnquiryDetail"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.SearchResult"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.LawDetail"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.AdvanceSearch"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Activity.VideoChannel"
            android:screenOrientation="portrait" >
        </activity>
        <activity android:name=".Activity.VideoViewer" >
        </activity>

        <receiver
            android:name=".GCM.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.example.oshpedia" />
            </intent-filter>
        </receiver>

        <service android:name=".GCM.GCMNotificationIntentService" />
    </application>

</manifest>

WhatsApp 并不完全依赖于 GCM(我不确定它是否完全依赖于 GCM)。如果您打开 Android 设备的“设置”并转到“应用程序”,然后转到“运行服务”(如果您使用的 Android 版本与我使用的 Android 版本不同,即 2.3.6,则菜单可能会略有不同),您会看到有一个WhatsApp 服务在后台运行(假设您安装了 WhatsApp)。如果您停止该服务,您将停止接收来自 WhatsApp 的消息,直到您再次启动该应用程序。

仅当您不明确终止 GCM 广播时,您的应用程序才能接受它。一旦你杀死它,它就不会收到任何广播,直到你下次启动它(根据我读到的,自 Android 3.x 以来就是这种情况)。但是,如果您将应用程序移至后台(例如,启动另一个应用程序或点击后退按钮,直到移至主屏幕或另一个应用程序),您的应用程序仍会收到来自 GCM 的消息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

应用程序未打开时,GCM 消息未发送? 的相关文章

随机推荐