【问题标题】:Android - Receive Push notification and display it - not quite understandingAndroid - 接收推送通知并显示它 - 不太了解
【发布时间】:2013-04-14 07:17:24
【问题描述】:

好的,我终于设置好注册设备以接收推送通知。我发现此代码可以接收新通知并显示它。事情是我不确定它会去哪里。我对 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()?
  • 我认为这可能是需要的,但是我是否在 onMessage 内部调用 generateNotification()?参数呢?例如generateNotification(这里的东西,这里的东西);我会放什么?
  • 哦,没关系,哇,我想我明白了。我开始感到迟钝,无论语言如何,功能都应该相同。我会试试看。这就是您复制和粘贴时发生的情况。大声笑

标签: android android-intent push-notification google-cloud-messaging


【解决方案1】:

我想通了。我终于找到了一个很好的工作项目,你可以研究它的代码并理解它。我花了几天的时间在谷歌上搜索,找到的不仅仅是这里的一块和那里的一块,而是一个完整的项目。我想我会很好地发布它以防其他人使用它。

https://github.com/Guti/Google-Cloud-Messaging--Titanium-

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多