即使应用程序关闭,仍然能够接收Android推送通知

7
我尝试使用Google GCM,但当应用程序关闭(从任务管理器中滑动或清除)时,它将无法接收到任何推送通知。当我再次打开应用程序时,通知已经消失并且丢失了。
GCM适用于: - 应用程序已打开 - 应用程序已最小化
不适用于: - 应用程序已关闭(从任务管理器中滑动) - 应用程序通过在任务管理器中清除所有打开的应用程序而关闭
我想即使应用程序已关闭也能接收到推送通知,就像Facebook或Instagram一样。我该如何实现这一点?这在GCM中是否可能?如果是,如何实现?如果不是,则还有其他方法可以实现吗?
以下是我的代码:
AndroidManifest.xml
<!-- [START gcm_receiver] -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.airwyntin.notificationtest" />
        </intent-filter>
    </receiver>
    <!-- [END gcm_receiver] -->

    <!-- [START gcm_listener] -->
    <service
        android:name=".MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->
    <!-- [START instanceId_listener] -->
    <service
        android:name=".MyInstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!-- [END instanceId_listener] -->
    <service
        android:name=".RegistrationIntentService"
        android:exported="false">
    </service>

MyGcmListenerService.java:

public class MyGcmListenerService extends GcmListenerService {


private static int notifId = 0;

private static final String TAG = "MyGcmListenerService";

/**
 * Called when message is received.
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("alert");


    Log.i(TAG, "From: " + from);

    if (message != null) {
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        if (from.startsWith("/topics/")) {
            // message received from some topic.
        } else {
            // normal downstream message.
        }

        // [START_EXCLUDE]
        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(message);
        // [END_EXCLUDE]
    }

}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, NotificationView.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("GCM Tesst Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    //Vibration
    notificationBuilder.setVibrate(new long[] { 0, 200, 200, 200, 200, 200 });


    //LED
    //notificationBuilder.setLights(Color.RED, 3000, 3000);

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

    Notification notif = notificationBuilder.build();
    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    /*notif.ledARGB = 0xFFff0000;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 100;
    notif.ledOffMS = 100;*/

    notificationManager.notify(notifId++ /* ID of notification */, notif);

}
}

MyInstanceIDListenerService.java:

public class MyInstanceIDListenerService extends InstanceIDListenerService {

private static final String TAG = "MyInstanceIDLS";

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. This call is initiated by the
 * InstanceID provider.
 */
// [START refresh_token]
@Override
public void onTokenRefresh() {
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
    Intent intent = new Intent(this, RegistrationIntentService.class);
    startService(intent);
}
// [END refresh_token]


}

RegistrationIntentService.java:

public class RegistrationIntentService extends IntentService {

private static final String TAG = "RegIntentService";
private static final String[] TOPICS = {"global"};

public RegistrationIntentService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    try {
        // [START register_for_gcm]
        // Initially this call goes out to the network to retrieve the token, subsequent calls
        // are local.
        // R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
        // See https://developers.google.com/cloud-messaging/android/start for details on this file.
        // [START get_token]
        InstanceID instanceID = InstanceID.getInstance(this);
        String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        // [END get_token]
        Log.i(TAG, "GCM Registration Token: " + token);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(token);

        // Subscribe to topic channels
        subscribeTopics(token);

        // You should store a boolean that indicates whether the generated token has been
        // sent to your server. If the boolean is false, send the token to your server,
        // otherwise your server should have already received the token.
        sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
        // [END register_for_gcm]
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        // If an exception happens while fetching the new token or updating our registration data
        // on a third-party server, this ensures that we'll attempt the update at a later time.
        sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
    }
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

/**
 * Persist registration to third-party servers.
 *
 * Modify this method to associate the user's GCM registration token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}

/**
 * Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
 *
 * @param token GCM token
 * @throws IOException if unable to reach the GCM PubSub service
 */
// [START subscribe_topics]
private void subscribeTopics(String token) throws IOException {
    GcmPubSub pubSub = GcmPubSub.getInstance(this);
    for (String topic : TOPICS) {
        pubSub.subscribe(token, "/topics/" + topic, null);
    }
}
// [END subscribe_topics]

}

你写过广播接收器吗? - Pradeep Deshmukh
只有广播接收器就足够了。 - Pradeep Deshmukh
可能是 https://dev59.com/MGjWa4cB1Zd3GeqPq3UK 的重复问题。 - abielita
@heyou 我正在使用华硕手机,它有一个自启动管理器。我尝试允许我的应用程序始终启动,之后我就可以始终接收到通知了。 - natsumiyu
我有同样的问题。你找到解决方案了吗? - Rodrigo Manguinho
显示剩余7条评论
3个回答

0
一个简单的解决方案是,在构建通知时“将优先级设置为高”。优先级常量的范围从-2到2(从最低到最高),0是此字段的默认值。希望这能有所帮助。谢谢。

0
根据GCM的官方声明,它与Google Play服务库一起工作,因此请尝试检查您的移动设备是否安装了最新版本的Google Play服务。有时候互联网连接问题会导致GCM推送通知出现问题,或者您可以检查交付收据以确定接收者是否真正收到了消息。 但是,您所说的当应用程序不在前台时GCM将无法工作是不正确的。

1
我有最新的和最新的Google Play服务。此外,互联网连接没有问题,我可以接收Facebook通知。当应用程序处于打开状态并且被最小化或在后台时,它将工作,但如果您关闭它,则不会再起作用。 - heyou

0

当用户强制关闭应用程序时:通知不会到达

这是Android平台的一个功能。用户通过强制停止应用程序会将其置于停止状态,它的任何代码都不会运行,包括在清单中声明的任何广播接收器broadcast receivers。只有当用户明确启动应用程序时,才会将其置于接收器被触发的状态。

有关强制停止的更多文档,请查看以下链接:


这不正确,因为WhatsApp通知到达正常。 - Samsung Developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接