如何在android中处理获取好友请求的推送通知



我想在android应用程序中为好友请求和消息实现推送通知,比如在facebook中。如果用户收到好友请求或收到消息,它会显示与我想做的完全相同的通知场景,尝试过谷歌,但没有找到解决方案,(请不要用GCM和PARSE推送通知来回答我)给我合适的教程链接或帮助我提供有价值的答案,提前谢谢。。。

更新了新答案

现在,您将不得不使用Firebase云消息,而不是旧的和不推荐使用的GCM:https://firebase.google.com/docs/cloud-messaging/

旧答案:

官方文件和您所有问题的答案如下:http://developer.android.com/google/gcm/gs.html

以下部分将指导您完成GCM的设置过程实施开始之前,请确保设置Google Play服务SDK。您需要此SDK才能使用GoogleCloudMessaging方法。

请注意,完整的GCM实现需要服务器端实现,除了应用程序中的客户端实现之外。本文档提供了一个完整的示例,其中包括和服务器。

由于你没有问什么具体的问题,我现在无法给出更好的答案,请告诉我们你不理解的地方,我们可能会帮助。。。

编辑:根据评论中的要求,这就是即使在后台运行时也显示通知的方式:

/**
 * Handling of GCM messages.
 */
public class GcmBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "GCMDemo";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context ctx;
    @Override
    public void onReceive(Context context, Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        ctx = context;
        String messageType = gcm.getMessageType(intent);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + intent.getExtras().toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification("Deleted messages on server: " +
                    intent.getExtras().toString());
        } else {
            sendNotification("Received: " + intent.getExtras().toString());
        }
        setResultCode(Activity.RESULT_OK);
    }
    // Put the GCM message into a notification and post it.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                new Intent(ctx, DemoActivity.class), 0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

最新更新