【问题标题】:GCM launch background/closed app when get notification (Android)GCM 在收到通知时启动后台/关闭的应用程序(Android)
【发布时间】:2016-03-17 18:58:43
【问题描述】:

我遇到了 GCM 问题。 我创建了一个服务器端通知推送器(在 PHP 中),它向拥有该应用程序的特定用户(基于位置)发送通知。

我有三个类来管理注册和通知监听器。

public class PusherIDListenerService extends InstanceIDListenerService {
    private static final String TAG = "PUSHER_ID_LISTENER_SERVICE";

    @Override
    public void onTokenRefresh() {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

现在是注册类

public class RegistrationIntentService extends IntentService {
    private static final String TAG = "REGISTRATION_SERVICE";

    private int idClient;

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        this.idClient = intent.getIntExtra("idClient", 0);
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        String locationProvider = LocationManager.NETWORK_PROVIDER;
        Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

        double lat = 0;
        double lon = 0;
        if (lastKnownLocation != null) {
            lat = lastKnownLocation.getLatitude();
            lon = lastKnownLocation.getLongitude();
        } else {
            Log.d(TAG, "Position not available, updating next time.");
        }

        try {
            synchronized (TAG) {
                InstanceID instanceID = InstanceID.getInstance(this);
                String senderId = getString(R.string.gcm_defaultSenderId);
                String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

                this.sendRegistrationToServer(token, lat, lon);
            }
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
        }

        Intent registrationComplete = new Intent(this, ActStartPage.class);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    private void sendRegistrationToServer(String token, double lat, double lon) {
        // doing some stuff
    }
}

现在是监听器

public class PusherListenerService extends GcmListenerService {
    private static final String TAG = "PUSHER_LISTENER_SERVICE";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String title = data.getString("title");
        String message = data.getString("body");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        sendNotification(title, message);
    }

    private void sendNotification(String title, String message) {
        Intent intent = new Intent(this, ActStartPage.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("menuTabId", 3);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

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

        notificationManager.cancelAll();
        notificationManager.notify((int)(Math.random()), notificationBuilder.build());
    }
}

我的清单看起来像这样

    <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" />
        </intent-filter>
    </receiver>
    <service
        android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.PusherListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.PusherIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <service
        android:name="clab.kalitys.mobile.CoreModuleManager.Pusher.RegistrationIntentService"
        android:exported="false">
    </service>

最后我在 onCreate 的主要活动中启动所有这些

    if (checkPlayServices()) {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        intent.putExtra("idClient", Integer.parseInt(this.getResources().getString(R.string.clientId)));
        startService(intent);
    }

那么现在,什么是有效的:

  • 启动注册:OK
  • 当应用处于前台或后台或从当前应用中删除时发送通知:OK
  • 当应用处于前台时,点击通知并再次启动应用:确定

什么不起作用 =( :

  • 当应用处于后台或从当前应用中删除时,单击通知并(再次)启动应用

我尝试了一些东西,但没有任何效果。我也不太了解 xml 设置。

这是我发送给 gcm 的 JSON 示例:

"notification":{
    "body":"blabla",
    "title":"one title"
    "icon":"notification_icon"
},
"data":{
    "body":"blabla",
    "title":"one title"
},
"registration_ids":["id1", "id2", etc]

那么当我在应用关闭时收到通知时再次启动应用,我的代码有什么问题(或者我必须添加什么)?

有什么想法吗?

谢谢 =)

【问题讨论】:

标签: android notifications google-cloud-messaging


【解决方案1】:

好的,

我刚刚解决了这个问题。

我发送的 JSON 是这样的:

"notification":{
    "body":"blabla",
    "title":"one title"
    "icon":"notification_icon"
},
"data":{
    "body":"blabla",
    "title":"one title"
},
"registration_ids":["id1", "id2", etc]

但这部分(来自 JSON):

"notification":{
    "body":"blabla",
    "title":"one title"
    "icon":"notification_icon"
}

是问题所在。

我认为 JSONmessage 分为两部分,应用打开时的“数据”和应用关闭或后台时的“通知”。 我想,因为应用程序已关闭,android怎么知道他必须显示通知?所以这就是为什么使用“通知”来指定android必须显示通知。

但是不,“通知”用于发送简单的通知而不采取行动。

所以我通过发送这个 JSON 来解决问题:

"data":{
    "body":"blabla",
    "title":"one title"
},
"registration_ids":["id1", "id2", etc]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多