即使App已关闭,也会收到Android推送通知

时间:2016-06-01 11:06:38

标签: android notifications google-cloud-messaging push

我尝试过使用Google GCM但是当应用关闭时(从任务管理器中滑动或清除),它不会收到任何推送通知。当我再次打开应用程序时,通知已经消失并丢失。

GCM正致力于: - 应用程序已打开 - 应用程序最小化

不工作: - 应用已关闭(从任务管理器滑动) - 通过清除任务管理器中所有打开的应用程序关闭应用程序

即使应用程序像Facebook或Instagram一样关闭,我也希望收到推送通知。我怎么能实现这个?这可能在GCM中有用吗?如果有,怎么样?如果没有,那么实现这个的另一种方法是什么?

这是我的代码:

的AndroidManifest.xml

    Image imageIcon = null;
    try {
        imageIcon = Image.createImage("/patternicot2.png");
    } catch (IOException e) {
        Log.p(e.toString());
    }
    Style style = form.getContentPane().getAllStyles();
    style.setBgImage(imageIcon);
    style.setBackgroundType(Style.BACKGROUND_IMAGE_TILE_BOTH);

MyGcmListenerService.java:

<!-- [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>

MyInstanceIDListenerService.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);

}
}

RegistrationIntentService.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]


}

3 个答案:

答案 0 :(得分:0)

根据GCM的官方声明,它适用于Google Play服务库,因此请尝试检查您的手机是否安装了Google Play服务的最新更新。有时互联网连接问题会导致网络导致GCM推送通知问题或者其他问题如果接收方确实收到了消息,您可以检查送货回执。 但是,当你的应用程序不在前台时,你说GCM无法正常工作

答案 1 :(得分:0)

当用户强行关闭应用时:通知无法到达

这是Android平台的一项功能。用户强制停止应用程序会使应用程序处于停止状态,并且不会运行任何代码,包括broadcast receivers中声明的任何manifest。只有当用户明确启动应用程序时,它才处于接收器被触发的状态。

有关Force Stop的更多文档,请点击以下链接:

答案 2 :(得分:0)

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