【问题标题】:android push notification not workingandroid推送通知不起作用
【发布时间】:2014-02-28 15:17:30
【问题描述】:

我创建了一个使用推送通知的 android 应用程序,但每次我从后端发送通知时,我的 android 平板电脑上都会弹出一个对话框,说“不幸的是 lic 已停止”。我尝试对应用程序进行故障排除,但似乎可以找到错误的来源。

我在 Manifest 中添加了以下权限:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
    android:name="com.example.myappname.pemermission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.myappname.permission.C2D_MESSAGE" />

我还注册了一个接收器来处理消息:

<receiver
    android:name="com.example.myappname.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.example.myappname"/>
    </intent-filter>
</receiver>

最后,我声明了一个新的meta-data 标签:

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity android:name="noti_handler"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden" />

我的Broadcastreceiver 代码如下:

public class GcmBroadcastReceiver extends BroadcastReceiver   {
@Override
public void onReceive(Context arg0, Intent arg) {
    String sh = gcm.getMessageType(arg);
    Bundle bb= arg.getExtras();
    if(!bb.isEmpty()){
    if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(sh)){
    String b=arg.getExtras().getString("message")+"<br>";
        String f="handler";

        try {
            FileOutputStream fos=openFileOutput(f, con.MODE_APPEND);
            fos.write(b.getBytes());
            fos.close();
        } catch (FileNotFoundException ef) {
            error(ef.toString());
            ef.printStackTrace();
        } catch (IOException e) {
            error(e.toString());
            e.printStackTrace();
        }
        catch(Exception ex){
            error(ex.toString());
        }

        sendNoti("you have a message/s");//this sends notification to user
    try{

    }
    catch(Exception e){
        Toast.makeText(getApplicationContext(), e.toString(),          Toast.LENGTH_LONG).show();   
    }

    }
    }
}


} 

【问题讨论】:

  • 发布 logcat 好友.....
  • 我正在使用设备而不是模拟器工作,我似乎无法在我的三星 Galaxy Tab3 上调试应用程序 - 无法从三星网站获取 oem 驱动程序

标签: java android push-notification broadcastreceiver google-cloud-messaging


【解决方案1】:

在您的 onReceive 方法中添加该行:

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

您在第 4 行有一个空指针执行。

编辑:

变化:

<activity android:name="noti_handler"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden" />

收件人:

<activity android:name="com.example.myappname.noti_handler"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden" />

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

收件人:

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

【讨论】:

  • 我已经在我的 oncreate 方法中这样做了——我只是把它放在我的 GcmBroadcastReceiver 类中,但是每次我向我的设备发送推送通知时,我仍然会显示相同的对话框
  • 请发布您的整个课程。
  • 所做的更改仍然无法正常工作 - 更改我的 noti_handler 活动的名称导致清单文件中的错误应该以“>”而不是“/>”结尾
  • 我还应该发布我的整个课程吗?真的需要让这个应用程序正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多