0

我大多是 android 技术的新手。我有一个 GCM 正在工作的项目。现在我将该项目与其他项目合并,GCM 刚刚停止工作......

我的服务正在正确调用 onRegistered 方法,但是当我向手机发送消息时,什么也没有发生。

有任何想法吗??这是 GCMBaseIntentService.java

package com.example.truque;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;


public class GCMIntentService extends GCMBaseIntentService {

    private static final String PROJECT_ID = "733836832755";

    private static final String TAG = "ACAAAAAAAAAAAA!!!!!!";

    public GCMIntentService()
    {
        super(PROJECT_ID);
        Log.d(TAG, "GCMIntentService init");
    }


    @Override
    protected void onError(Context ctx, String sError) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Error: " + sError);

    }

    @Override
    protected void onMessage(Context ctx, Intent intent) {

        Log.d(TAG, "Message Received");

        String message = intent.getStringExtra("message");

        sendGCMIntent(ctx, message);

    }


    private void sendGCMIntent(Context ctx, String message) {

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("GCM_RECEIVED_ACTION");

        broadcastIntent.putExtra("gcm", message);

        ctx.sendBroadcast(broadcastIntent);
        /////////////////////////////////////////////////////////////////
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);

        int icon = R.drawable.ic_launcher;        
        CharSequence tickerText = "Leeme"; // ticker-text
        long when = System.currentTimeMillis();         
        Context con = ctx.getApplicationContext();     
        CharSequence contentTitle = "Mensaje recibido";  
        CharSequence contentText = message;      
        Intent notificationIntent = new Intent(ctx, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(con, contentTitle, contentText, contentIntent);
        notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        // and this
        int HELLO_ID = 1;
        mNotificationManager.notify(HELLO_ID, notification);

        File f = new File("messages.txt");
        if(!f.exists()){
            try {
                FileOutputStream fos = ctx.openFileOutput("messages.txt", Context.MODE_APPEND);

                String c=getCanal(message);
                String n=getNick(message);
                String m=getMensaje(message);
                fos.write(("["+(new Date()).toString()+"]["+c+"]["+n+"] "+m).getBytes());
                fos.write(System.getProperty("line.separator").getBytes());
                fos.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

        ///////////////////////////////////////////////////////////////////
    }

    String getCanal(String m){
        return "Canal N";
    }
    String getNick(String m){
        return "Nick N";
    }
    String getMensaje(String m){
        return m;
    }

    @Override
    protected void onRegistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send regId to your server
        Log.d(TAG, regId);

    }

    @Override
    protected void onUnregistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send notification to your server to remove that regId
        Log.d("Unregistered ACAAAAA!!", regId);
    }

}

和 AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <permission
        android:name="com.example.truque.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.truque.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 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" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_OWNER_DATA" />

    <!-- wake the processor if a GCM 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="com.example.truque.MainActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.example.truque.LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="adjustResize|stateVisible" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.truque.AltaUsuario"
            android:label="@string/title_activity_alta_usuario" >
        </activity>
        <activity
            android:name="com.example.truque.Notis"
            android:label="@string/title_activity_notis"
            android:theme="@android:style/Theme.Dialog" >
        </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="com.example.truque" />
            </intent-filter>
        </receiver>
        <service 
            android:name=".GCMIntentService" >
        </service>
        <receiver
            android:name="com.example.truque.PushReceiver"
            android:enabled="true"
            android:exported="true" >
        </receiver>
    </application>

</manifest>
4

0 回答 0