1

我想向我的 android 设备发送通知。问题是在正确发送和接收的第一个通知之后,我必须关闭(杀死)并打开应用程序才能接收另一个通知。我不知道我必须改变什么。非常感谢你

我像这样对应用程序进行了编程(如谷歌示例):

public class FirstActivity extends Activity
{
    String TAG = "FirstActivity";
    String SENDER_ID = "123456789101";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, SENDER_ID);
        } else {
          Log.v(TAG, "Already registered");
        }
    }
}

和:

public class GCMIntentService extends GCMBaseIntentService
{
    String TAG = "GCMIntentService";
    public GCMIntentService()
    {
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onError(Context arg0, String arg1)
    {
        // TODO Auto-generated method stub
        Log.d(TAG, "onError");
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1)
    {
        // TODO Auto-generated method stub
        Log.d(TAG, "onMessage");
        generateNotification(arg0, arg1.getStringExtra("message"));

    }
    private static void generateNotification(Context context, String message) {
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context,
                FirstActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }


    @Override
    protected void onRegistered(Context arg0, String arg1)
    {
        // TODO Auto-generated method stub
        Log.d(TAG, "onRegistered");
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1)
    {
        // TODO Auto-generated method stub
        Log.d(TAG, "onUnregistered");
    }

}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="at.demo.gcm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <permission
        android:name="at.demo.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="at.demo.gcm.permission.C2D_MESSAGE" />

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="at.demo.gcm.FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="at.demo.gcm" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />
    </application>

</manifest>
4

2 回答 2

2

GCMIntentService 类的构造函数必须具有 SENDER_ID,在您的情况下,如:

private static final String SENDER_ID = "123456789101";

public GCMIntentService()
    {
        super(SENDER_ID);
    }

希望这会对您有所帮助,因为我在我正在使用的类似应用程序中尝试了您的代码,并且只要我从 Web 服务器发送通知而无需终止应用程序,我就会收到通知

于 2012-12-01T14:22:54.697 回答
0

GCM 在这里不是问题。问题是您如何生成通知(调试应该显示)。您为所有通知使用一个 id 0,您无法通过这种方式更新通知。每次都换身份证。

于 2012-12-01T14:17:36.170 回答