【问题标题】:Notifications Code in AndroidAndroid中的通知代码
【发布时间】:2015-11-17 17:29:04
【问题描述】:

我正在尝试在按钮中编写一个简单的通知代码,当我按下它时会创建一个通知,但是当我按下按钮时通知没有出现这里是我正在使用的代码

int notificationId = 001;
        // Build intent for notification content
        Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
        PendingIntent viewPendingIntent =
                PendingIntent.getActivity(SessionsActivity.this, 0, viewIntent, 0);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(SessionsActivity.this)
                        .setContentTitle("hii")
                        .setContentText("hii2")
                        .setContentIntent(viewPendingIntent);
        // Get an instance of the NotificationManager service
        NotificationManagerCompat notificationManager =NotificationManagerCompat.from(SessionsActivity.this);
        // Build the notification and issues it with notification manager.
        notificationManager.notify(notificationId, notificationBuilder.build());

【问题讨论】:

    标签: java android notifications


    【解决方案1】:
    Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    
    
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    notificationBuilder.setStyle(inboxStyle);
    inboxStyle.setBigContentTitle("sdjshfjnds");
    inboxStyle.addLine("sdjjsdfn");
    notificationBuilder.setStyle(inboxStyle);
    
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int NOTIFICATION_ID = 100;
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-03
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多