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);
    }

私が作業している同様のアプリであなたのコードを試してみたので、これがあなたの助けになることを願っています.

于 2012-12-01T14:22:54.697 に答える
0

ここでは GCM は問題ではありません。問題は、generateNotification をどのように行ったかです (デバッグによって明らかになるはずです)。すべての通知0に 1 つの ID を使用しています。この方法で通知を更新することはできません。IDは毎回変えます。

于 2012-12-01T14:17:36.170 に答える