推送通知在Android [英] Push notification on Androïd

查看:229
本文介绍了推送通知在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建上,我想要实现推送通知的应用程序。
由于很多教程我已经创建的服务器部分和客户端部分。

在服务器部分,我送与卷曲(在PHP)POST请求。
服务器响应: <$c$c>{\"multicast_id\":5560733296047502303,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1408610711700937%6f027011f9fd7ecd\"}]}

但我没有收到我的手机上的通知。

在applicatoin我有一个服务:
的Manifest.xml

 &LT;服务机器人:名字=。GCMIntentService/&GT;

和所有需要的权限。

GCMIntentService.java

 保护无效的onMessage(上下文的背景下,意图意图){
    INT图标= R.drawable.ic_launcher;
    时长= System.currentTimeMillis的();    NotificationManager notificationManager =(NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    通知通知=新的通知(图标,消息,时);    字符串title = context.getString(R.string.app_name);    意图notificationIntent =新意图(背景下,MainActivity.class);
    //设置的意图,因此它不会启动新活动
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    的PendingIntent意图=
    PendingIntent.getActivity(上下文,0,notificationIntent,0);
    notification.setLatestEventInfo(上下文,标题,邮件,意图);
    notification.flags | = Notification.FLAG_AUTO_CANCEL;    //播放默认通知声音
    notification.defaults | = Notification.DEFAULT_SOUND;    //notification.sound = Uri.parse(android.resource://+ context.getPackageName()+your_sound_file_name.mp3);
    //如果振动启用震动
    notification.defaults | = Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0,通知);
}

我如何调试呢?或者是有什么明显的原因是什么?


解决方案

在设备,需要执行以下步骤:


  1. 注册Google云端通讯。

    Google云端通讯GCM = GoogleCloudMessaging.getInstance(背景);
    字符串gcmRegistrationId = gcm.register();


  2. 发送gcmRegistrationId在步骤1到服务器了。服务器将使用该ID发送GCM挠痒痒。


  3. 注册GCM接收机。
    在AndroidManifest.xml中添加如下:



 &lt;接收
    机器人:名字=com.hp.msa.receiver.GCMReceiver
    机器人:出口=真
    机器人:权限=com.google.android.c2dm.permission.SEND&GT;
    &所述;意图滤光器&gt;
        &lt;作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/&GT;
        &lt;作用机器人:名字=com.google.android.c2dm.intent.REGISTRATION/&GT;
    &所述; /意图滤光器&gt;
&LT; /接收器&GT;


GCMReceiver.java看起来如下:

 公共类GCMReceiver扩展WakefulBroadcastReceiver {
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){        //在这里,你会得到实际的推送通知。
        //接受它后,就可以执行任务
        //意图包含数据服务器发送            Google云端通讯GCM = GoogleCloudMessaging.getInstance(本);            //该getMessageType()意图参数必须是您收到的意图
            //你的广播接收器。            字符串为messageType = gcm.getMessageType(意向);        如果(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(为messageType)){
                //逻辑
        }否则如果(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(为messageType)){
                //逻辑
        }否则如果(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(为messageType)){
                //逻辑
        }
       }
}

I create an application on which I want to implement Push Notifications. Thanks to many tutorials I've create the server part and the client part.

On the server part, I send a POST request with cURL (in PHP). The server responds : {"multicast_id":5560733296047502303,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1408610711700937%6f027011f9fd7ecd"}]}

But I don't receive the notification on my phone.

In the applicatoin I have a service : Manifest.xml

<service android:name=".GCMIntentService" />

And all needed permissions.

GCMIntentService.java

protected void onMessage(Context context, Intent intent) {
    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, MainActivity.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;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://"+ context.getPackageName()+ "your_sound_file_name.mp3");
    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);        
}

How can I debug this ? Or is there any obvious reason ?

解决方案

On Device, you need to perform following steps :

  1. Register to GoogleCloudMessaging.

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String gcmRegistrationId = gcm.register();

  2. Send gcmRegistrationId got in step 1 to Server. Server will use this id to send GCM tickle.

  3. Register GCM Receiver. Add it in AndroidManifest.xml as below :

<receiver
    android:name="com.hp.msa.receiver.GCMReceiver"
    android:exported="true"
    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" />
    </intent-filter>
</receiver>

GCMReceiver.java will look as below :

public class GCMReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

        // Here, you will get actual PUSH notification.
        // After receiving it, you can perform your tasks
        // Intent contains data sent by server  

            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

            // The getMessageType() intent parameter must be the intent you received
            // in your BroadcastReceiver.

            String messageType = gcm.getMessageType(intent);  

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                // Logic
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                // Logic
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // Logic
        }
       }
}

这篇关于推送通知在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆