Push notification 从服务不工作发出的通知打开活动

Push notification 从服务不工作发出的通知打开活动,push-notification,android-pendingintent,Push Notification,Android Pendingintent,我的应用程序从我设置的应用程序服务器接收到通知,试图打开活动时遇到了非常糟糕的情况。我有一个android服务,当用户登录到应用服务器时运行,该服务器负责接收通知。当用户单击通知时,应该启动一个名为Notifications的活动。Android服务上的相关代码: int requestID = (int) System.currentTimeMillis(); Intent intent = new Intent(); intent.setComponent(new ComponentNam

我的应用程序从我设置的应用程序服务器接收到通知,试图打开活动时遇到了非常糟糕的情况。我有一个android服务,当用户登录到应用服务器时运行,该服务器负责接收通知。当用户单击通知时,应该启动一个名为Notifications的活动。Android服务上的相关代码:

int requestID = (int) System.currentTimeMillis();

Intent intent = new Intent();
intent.setComponent(new ComponentName(this, Notifications.class));      
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//intent.putExtra("json", message);
PendingIntent pintent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
然后,在设置通知的标题和文本后:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.plus)
            .setContentTitle(title)
            .setContentText(caption)
            .setContentIntent(pintent)
            .setLights(Color.BLUE, 1000, 1000)
            .setVibrate(new long[] {0, 100, 100, 100});

NotificationManager mNotifyMgr = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(100, mBuilder.build());
04-26 23:43:33.301: D/StatusBar(896): Clicked on content of 0|com.myapp|100|null|10107
04-26 23:43:33.312: I/ActivityManager(725): START u0 {flg=0x10008000 cmp=com.myapp/com.google.android.gms.games.Notifications (has extras)} from uid 10107 on display 0
04-26 23:43:33.315: D/audio_hw_primary(189): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
04-26 23:43:33.322: W/InputMethodManagerService(725): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@6bbbbe3 attribute=null, token = android.os.BinderProxy@2c3766bc
04-26 23:43:33.326: D/audio_hw_primary(189): select_devices: out_snd_device(2: speaker) in_snd_device(0: none)
04-26 23:43:33.326: D/msm8974_platform(189): platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15)
04-26 23:43:33.326: D/audio_hw_primary(189): enable_snd_device: snd_device(2: speaker)
04-26 23:43:33.330: D/audio_hw_primary(189): enable_audio_route: apply and update mixer path: low-latency-playback
04-26 23:43:33.703: D/PhoneStatusBar(896): disable: < expand icons* alerts system_info* back home recent clock search >
整个通知活动包括:

package com.myapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class Notifications extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notifications);
    }
}
我已在清单文件中声明了我的活动:

<activity
    android:name=".Notifications"
    android:label="@string/title_activity_notifications"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"
    android:taskAffinity=""
    android:excludeFromRecents="true">
</activity>
我感到困惑的是,这个特定的活动将不会打开-我已经尝试将它设置为打开其他活动,然后它就可以正常工作了。这是我点击通知时的日志:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.plus)
            .setContentTitle(title)
            .setContentText(caption)
            .setContentIntent(pintent)
            .setLights(Color.BLUE, 1000, 1000)
            .setVibrate(new long[] {0, 100, 100, 100});

NotificationManager mNotifyMgr = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(100, mBuilder.build());
04-26 23:43:33.301: D/StatusBar(896): Clicked on content of 0|com.myapp|100|null|10107
04-26 23:43:33.312: I/ActivityManager(725): START u0 {flg=0x10008000 cmp=com.myapp/com.google.android.gms.games.Notifications (has extras)} from uid 10107 on display 0
04-26 23:43:33.315: D/audio_hw_primary(189): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
04-26 23:43:33.322: W/InputMethodManagerService(725): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@6bbbbe3 attribute=null, token = android.os.BinderProxy@2c3766bc
04-26 23:43:33.326: D/audio_hw_primary(189): select_devices: out_snd_device(2: speaker) in_snd_device(0: none)
04-26 23:43:33.326: D/msm8974_platform(189): platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15)
04-26 23:43:33.326: D/audio_hw_primary(189): enable_snd_device: snd_device(2: speaker)
04-26 23:43:33.330: D/audio_hw_primary(189): enable_audio_route: apply and update mixer path: low-latency-playback
04-26 23:43:33.703: D/PhoneStatusBar(896): disable: < expand icons* alerts system_info* back home recent clock search >
它是否与logcat中的com.google.android.gms.games.Notifications文本有关?我在Nexus5上运行Android 5.1

编辑
因此,我将活动重命名为通知,现在一切正常。首先,我没有一个合适的解释来解释它为什么会失败,所以我将把这个问题留给/如果有人能够充分解释一开始发生了什么,因为我现在不打算再花时间在它上面了。

发生了什么,这是非常清楚的。您可以使用以下代码设置通知意图:

您必须已导入包com.google.android.gms.games,以便编译器使用com.google.android.gms.games.Notifications而不是您自己的类。当您更改类的名称时,编译器能够选择正确的类。请看一下您的导入语句