GCM唤醒许可

时间:2015-10-14 20:52:13

标签: android google-cloud-messaging

在我看到谷歌关于效果模式的视频 - video后,我决定实施Google云消息传递。 但是当我为Android实现GCM时,我看到GCM正在使用一些权限,其中一个是“唤醒锁”。 但就像我们所知道的那样,这种许可与电池消耗“等于”。

所以我的问题是,我们如何管理这个问题? Lib GCM为我们这样做?使用拉动通知比拉动更好吗?

由于

2 个答案:

答案 0 :(得分:0)

你不必这样做。 Google提供的服务可以为您处理所有事情。如下所示:

 public class GcmListenerService extends com.google.android.gms.gcm.GcmListenerService {

private static final String TAG = "GcmListenerService";

/**
 * 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("message");
    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, TabAllItemsActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, Resources.GCM_NOTIFICATION_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_notification)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_app))
            .setContentTitle("Message from Faroque")
            .setContentText(message)
            .setColor(R.color.orange_dark)//setting the brand color
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);//This intent will be executed when user tap on it

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

    notificationManager.notify(Resources.GCM_NOTIFICATION_ID, notificationBuilder.build());
}

}

Here是一个教程,您可以检查是否需要。感谢

答案 1 :(得分:0)

定义唤醒锁许可不会导致电池耗尽。滥用唤醒锁的确如此。 GcmListenerService为您管理唤醒锁,这是使用GcmListenerService是一个好主意的原因之一,因为您不必自己管理唤醒锁(这是非平凡的)。