安卓-接收推送通知并显示-不太理解

3

好的,我终于设置好了推送通知设备的注册。我找到了这个代码来接收新的通知并将其显示出来。问题是我不确定它应该放在哪里。我对Android编程还比较陌生,所以需要帮助。

我有一个名为GCMService的服务类如下。

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMService extends GCMBaseIntentService {

    private static final String TAG = "GCMService";

    public GCMService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String regId) {
        Log.i(TAG, "Device registered: regId= " + regId);
    }

    @Override
    protected void onUnregistered(Context context, String regId) {
        Log.i(TAG, "Device unregistered");
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        Log.i(TAG, "Received recoverable error: " + errorId);
        return super.onRecoverableError(context, errorId);
    }
}

下面这个函数应该放在上面所述的类中的哪里,才能接收到新消息?
private static void generateNotification(Context context, String message) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager) 
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(icon, message, when);      

    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, LauncherActivity.class);

      PendingIntent pintent = PendingIntent.getActivity(context, 0, intent,
 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(1, notification);
}

你尝试过在通知到达时调用你的通知生成函数 - GCMService.onMessage() 吗? - Morrison Chang
我想这可能是需要的,但我是否只在onMessage内调用generateNotification()呢?参数又该怎么办?例如:generateNotification(这里放点东西, 这里也放点东西); 我该放些什么? - Panama Jack
哦,算了吧,哇,我想我明白了。我开始感觉有点傻,一个函数应该在任何语言下都能工作。我会尝试一下,看看结果如何。这就是当你只是复制粘贴时会发生的事情。哈哈 - Panama Jack
2个回答

3
我搞定了。我最终找到了一个相当不错的工作项目,你可以学习其中的代码并理解它。我花了几天时间在谷歌上搜索,才找到了一个完整的项目,而不仅仅是一块一块的零碎代码。我想发帖分享出来,以便其他人也能够使用。 https://github.com/Guti/Google-Cloud-Messaging--Titanium-

1

使用以下代码,它可以正常工作:

@Override
public void onDeletedMessages() {

    sendNotification("Message Deleted On server");
    super.onDeletedMessages();
}

@Override
public void onMessageReceived(String from, Bundle data) {

    sendNotification("Received: " + data.getString("message"));
    super.onMessageReceived(from, data);
}

@Override
public void onMessageSent(String msgId) {

    sendNotification("Message Sent: " + msgId);
    super.onMessageSent(msgId);
}

@Override
public void onSendError(String msgId, String error) {

    sendNotification("Message Sent Error: " + msgId + "Error: " + error);
    super.onSendError(msgId, error);
}

private void sendNotification(String msg) {

    Intent intent = new Intent(this, MessageView.class);
    intent.putExtra("Message", msg);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.logo_y)
            .setContentTitle("CCD Message").setContentText(msg)
            .setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接