获取通知时,GCM启动后台/关闭应用程序(Android)

时间:2016-03-15 15:34:15

标签: android notifications google-cloud-messaging

我遇到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);
    }

现在,有效的是:

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

什么不起作用=(:

  • 当应用在后台或从当前应用中删除时,点击通知并再次启动应用

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

以下是我发送给gcm的JSON示例:

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

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

有什么想法吗?

谢谢=)

1 个答案:

答案 0 :(得分: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"
}

是问题所在。

我认为JSON消息分为两部分,&#34;数据&#34;当应用程序打开时,&#34;通知&#34;当应用关闭或在后台时。 我认为,因为应用程序已关闭,android怎么知道他必须显示通知?这就是为什么使用&#34; notification&#34;指定android必须显示通知。

但不,&#34;通知&#34;用于发送简单通知而无需操作。

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

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