Android 2.3中未收到通知

时间:2014-10-28 11:05:20

标签: android android-notifications

我正在实施通知服务,我在Android 2.3中收到消息时遇到问题。我收到版本4.0及更新版本但不是2.3版本的邮件。在logcat中出现以下错误:

无法找到com.google.android.gms.ads.internal.n.e.a方法引用的“com.google.android.gms.ads.internal.b.c”类 。 。 找不到类'android.app.Notification $ Builder',引自com.google.android.gms.common.l.a方法

可能是什么问题?这是我发送通知的方法:

    private void sendNotification(String msg) {

    MainActivity.notificationClicked = true;

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    String strMessage = loadPreferences();
    String newMessage = "";

    if (!strMessage.isEmpty())
        newMessage = strMessage + "<br>" + msg;
    else
        newMessage = msg;

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
    contentView.setTextViewText(R.id.text, Html.fromHtml(newMessage));


    Notification notification = new Notification(icon, Html.fromHtml(msg), when);
    notification.contentView = contentView;
    String title = this.getString(R.string.app_name);


    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

    List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

    Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClass().getSimpleName());

    ComponentName componentInfo = taskInfo.get(0).topActivity;

    Intent notificationIntent;

    if(!componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){
        notificationIntent = new Intent(getApplicationContext(),
                FirstActivity.class);
    } else {
        notificationIntent = new Intent(getApplicationContext(),
                MainActivity.class);

        notificationIntent.putExtra("login", true);
    }
        notificationIntent.putExtra("message", Html.fromHtml(msg));

        oldMessage = newMessage;


        savePreferences(getApplicationContext(), oldMessage);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

        int notifyID = 1;

        PendingIntent intent = PendingIntent.getActivity(this, notifyID,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setLatestEventInfo(this, title, msg, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.contentIntent = intent;

        notificationManager.notify(notifyID, notification);

}

当我发送消息时,我会在Android 4.2的设备中收到该消息,但在Android 2.3的设备中却没有。我正在使用Android 2.3调试应用程序,我在IntentService的onHandleIntent方法中放了一个断点,但它永远不会出现。我认为这可能是针对logcat中出现的错误。

如何解决Android 2.3设备的问题?

提前致谢。


我将通知更改为NotificationCompat:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setContentIntent(intent); Notification notification = mBuilder.build(); notification.contentView = contentView;
notificationManager.notify(notifyID, notification);

但问题仍然存在,在logcat中出现相同的错误

1 个答案:

答案 0 :(得分:0)

试试这段代码。我在我的项目中使用过这个并且运行良好。 receiverActivity是一个活动,会打开onclick,我记得,但我不是100%肯定它。

public static void showNotification ( Context context, Class receiverActivityClass, String title, String text, int icon )
    {
        Intent intent = new Intent( context, receiverActivityClass );
        PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, 0 );

        // Build notification
        Notification notification = new Notification.Builder( context )
                .setContentTitle( title )
                .setContentText( text )
                .setSmallIcon( icon )
                .setContentIntent( pendingIntent )
                .setAutoCancel( true )
                .getNotification();


        NotificationManager notificationManager =
                ( NotificationManager ) context.getSystemService( context.NOTIFICATION_SERVICE );
        notificationManager.notify( 0, notification );
    }